473,699 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need some help for my program

1 New Member
I'm writing a program that determine if that is prime or perfect numbers and output factors of them. If 6 is the first input, the output will say it's a perfect number but if i input 6 again it says 6 is not a prime. Could use some help here. Thanks!
Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. using namespace std;
  3. int main ()
  4. {
  5.     int num, range=1000;
  6.     int n;
  7.     int divisor;
  8.     int sum=0;
  9.  
  10.    const int SENTINEL=-1;    
  11.     cout<<"enter integers between 1 and 1000 <enter -1 to exit> :";
  12.     cin>>num;                          
  13.       if(0<num && num<=range)
  14.     {           
  15.  
  16.     while(num!=SENTINEL)    
  17.     {         
  18.         for ( n=2; n < num; n++ ) 
  19.             if (num%n==0 )        
  20.  
  21.                 break;                             
  22.             if ( n==num )      
  23.                 cout << num<<" is prime"<< endl;
  24.             else 
  25.             {   
  26.                 cout << num << " is not prime" <<endl;
  27.                 for(divisor=1; divisor <= num/2; divisor++)                    
  28.                     if (( num%divisor==0) && (num!=divisor))
  29.                     {
  30.                         cout << "The factor of " << num << " is: " << divisor << endl;
  31.                        sum +=divisor ; 
  32.  
  33.                          if(sum==num)
  34.                          cout << num << " is a perfect number" << endl;                        
  35.                     }                
  36.             }          
  37.             cin>>num;          
  38.     }     
  39.     }
  40.  
  41.     else
  42.     cout<<"invalid Number, number should be between 1 and 1000";      
  43.     return 0;
  44. }
Oct 26 '07 #1
1 1538
amitpatel66
2,367 Recognized Expert Top Contributor
I'm writing a program that determine if that is prime or perfect numbers and output factors of them. If 6 is the first input, the output will say it's a perfect number but if i input 6 again it says 6 is not a prime. Could use some help here. Thanks!

#include <iostream>
using namespace std;
int main ()
{
int num, range=1000;
int n;
int divisor;
int sum=0;

const int SENTINEL=-1;
cout<<"enter integers between 1 and 1000 <enter -1 to exit> :";
cin>>num;
if(0<num && num<=range)
{

while(num!=SENT INEL)
{
for ( n=2; n < num; n++ )
if (num%n==0 )

break;
if ( n==num )
cout << num<<" is prime"<< endl;
else
{
cout << num << " is not prime" <<endl;
for(divisor=1; divisor <= num/2; divisor++)
if (( num%divisor==0) && (num!=divisor))
{
cout << "The factor of " << num << " is: " << divisor << endl;
sum +=divisor ;

if(sum==num)
cout << num << " is a perfect number" << endl;
}
}
cin>>num;
}
}

else
cout<<"invalid Number, number should be between 1 and 1000";
return 0;
}
Why is yur code with lots of for and if conditions just to find if it is prime or not??
a single for loop with running through num/2 and checking num%n ==0 will help you find out the prime or not.
Have a flag = 0, set flag = 1 if num%n =0 then finally check if flag = 0 then number is prime else not prime....
Oct 26 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2258
by: Jung Joon Park | last post by:
Hi everyone. I am JJ Park. I got some questions in C++. First of all, my English is not perfect, because I am foreigner. I have got C++ code, which have no syntex error in Visual C++ ver6.0 compiling, however, this code was made in Sun systems (UNIX based). I shall change the C++ code unix > Window based. And this code contain "calling another program for data calculating and return results for calculating in next steps in C++...
1
1901
by: Anthony Esochaghi | last post by:
I need some constructive input to be able to finish my program. I have a field called (1) DateRec, another (2) Municipality and another (3) Type. These three fields invariably will automatically generate a string of number for another field that is called FileNumber. This FileNumber is already the Primary Key in the program. The (1) is the date received, that is the date the application of the client was received. (2) The...
13
2460
by: vgame64 | last post by:
Hi, I have been struggling with writing a program for a few hours. The requirements are that: """You will be writing a program which will determine whether a date is valid in terms of days in that month. We are assuming that the year will be valid 4 digit integer. So you don't have to think much about that(in terms of validation) except for the month of February. If the month is February, then you have to check whether that year is Leap...
13
3253
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else happend. I think the problem may be in my input function or in the main function. If anyone out there can help me it woul be greatly appreciated. Here is the code: #include <iostream>
4
2210
by: robinsand | last post by:
My apologies to those of you who are more advanced Visual C++ .NET programmers, but I am working on a project for an MBA course that is condensed into an eight-week schedule, and I need help getting a program up and running with proper files and documentation to be handed in for a grade (on Microsoft Visual Studio .NET 2003). I am being graded on how well I incorporate advanced C++ features such as inheritance, polymorphic programming,...
16
2531
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client uses IE to talk with a server. The user on the client (IE) sees an ASP net page containing a TextBox. He can write some text in this text box and push a submit button.
8
2744
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only the sequence nos and it should be such that i can access it through the structure .plz help me .
4
2199
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
0
1243
by: RENEE | last post by:
I Need help writing a program that will process payroll for small company with 26 employess, needs to store all employee name in sequential file along with address info, pay rate, number of dependents, martial status and a dollar amount which is a deduction for the company 401k plan. part one of the program allows one to add employee to the sequential file. part two of the program reads the sequential file, and for the each employee, ask for...
21
2543
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to run, However, i simply created this program in arrays and it runs good except i cant figure out how to compute the standard deviation. The coding is below. Any help will be appreciated. 1) The Program will prompt the user for six grades to be...
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.