473,320 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Need some help for my program

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 1528
amitpatel66
2,367 Expert 2GB
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!=SENTINEL)
{
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
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...
1
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...
13
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...
13
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...
4
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...
16
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...
8
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...
4
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...
0
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...
21
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.