473,394 Members | 2,063 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,394 software developers and data experts.

Program to calculate prime and perfect numbers

i am a begginer in c++ programming and im stuck at using loops so plz help me!!!

i need to write a program that analyze one or more # (between 1 and 1000) entered by a user. Then determin if it is a prime number or perfect number.
And i have to use the loops at a bigginer level. so plz help me to get through this with a simple program!!!!!!!!!!!!TANX
Oct 21 '07 #1
19 8531
Ganon11
3,652 Expert 2GB
We'd be glad to help you through it - so what do you have so far?
Oct 22 '07 #2
ok i have the code for finding the devisor of any number!!!!!i just cant find how to write a code that show the number entered by user is a prime number?and also how to get the sum of factors for perfect number??????TANX FOR YOU HELPS
so far i have this basic code~~~~dont laugh!!!

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. include<cmath>
  3. using namespace std;
  4. int main()
  5. {
  6. int num,root,dev;
  7. const int SENTINEL=-1;                                //constant and integers
  8. cout<<"ENTER #<enter -1 to stop>:";
  9.  
  10.     cin>>num;                                   //# entered by user
  11.     while(num!=SENTINEL)                 
  12. {
  13.     root=pow(num,0.5);                       //second root of that #
  14.     cout<<"devisors are: 1"<<" "<<num<<" ";
  15.     for (root;1<root;root--)
  16. {
  17.         if(num%root==0)                                  //the loop and conditions
  18. {
  19.     dev=num/root;
  20.     cout<<root<<" "<<dev<<" ";
  21.  
  22. }
  23.        else
  24.     continue;                
  25. }
  26.     cout<<endl;
  27.     cin>>num;
  28. }
  29.     return 0;
  30. }
Oct 22 '07 #3
Heres a hint you should try using the mod operator"%"..
It will give you single digits for perfect number evaluation.
And prime is repeated use of operator "mod" until remainder is not equal to zero
Oct 22 '07 #4
Tanx but i still need to fin a code for adding the devisors (except the # itself).
and a code for determining a entered number as a prime or perfect or just a number!!!!
Oct 22 '07 #5
Banfa
9,065 Expert Mod 8TB
For determining perfect numbers in the users input the most efficient method would be to use a look-up table. This is beacuse there are only 5 perfect numbers with-in range for a 32-bit int and only 8 in range of a 64 bit int.

Perfect numbers are very rare and you can easily look up a list of the lower ones on the internet.
Oct 22 '07 #6
sicarie
4,677 Expert Mod 4TB
Tanx but i still need to fin a code for adding the devisors (except the # itself).
and a code for determining a entered number as a prime or perfect or just a number!!!!
Why do you want to add the divisors? Is that part of the assignment?

Also, given the site's rules on homework, what have you tried on this?
Oct 22 '07 #7
Banfa
9,065 Expert Mod 8TB
Why do you want to add the divisors? Is that part of the assignment?
Part of the definition of a perfect number which is for a number n

if 2n = SUM(All divisors of n) then n is perfect.

if 2n < SUM(All divisors of n) then n is deficient.

if 2n > SUM(All divisors of n) then n is abundent.
Oct 22 '07 #8
sicarie
4,677 Expert Mod 4TB
Part of the definition of a perfect number which is for a number n

if 2n = SUM(All divisors of n) then n is perfect.

if 2n < SUM(All divisors of n) then n is deficient.

if 2n > SUM(All divisors of n) then n is abundent.
Ah. Okay. (Sorry, next time I'll research the familiar-sounding terms ;). Well that shouldn't be too difficult, could use an array or list to hold any of the divisors...
Oct 22 '07 #9
Part of the definition of a perfect number which is for a number n

if 2n = SUM(All divisors of n) then n is perfect.

if 2n < SUM(All divisors of n) then n is deficient.

if 2n > SUM(All divisors of n) then n is abundent.

Tanx Banfa that helped alot. the only thing i need is a code for determining prime numbers in the users inputs.
Oct 22 '07 #10
Banfa
9,065 Expert Mod 8TB
Part of the definition of a perfect number which is for a number n

if 2n = SUM(All divisors of n) then n is perfect.

if 2n < SUM(All divisors of n) then n is deficient.

if 2n > SUM(All divisors of n) then n is abundent.
Eeeek I have detected an error the correct forumlas are

if 2n = SUM(All divisors of n) then n is perfect.

if 2n > SUM(All divisors of n) then n is deficient.

if 2n < SUM(All divisors of n) then n is abundent.
Oct 22 '07 #11
ok i have the code for finding the devisor of any number!!!!!i just cant find how to write a code that show the number entered by user is a prime number?and also how to get the sum of factors for perfect number??????TANX FOR YOU HELPS
so far i have this basic code~~~~dont laugh!!!

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. include<cmath>
  3. using namespace std;
  4. int main()
  5. {
  6. int num,root,dev;
  7. const int SENTINEL=-1;                                //constant and integers
  8. cout<<"ENTER #<enter -1 to stop>:";
  9.  
  10.     cin>>num;                                   //# entered by user
  11.     while(num!=SENTINEL)                 
  12. {
  13.     root=pow(num,0.5);                       //second root of that #
  14.     cout<<"devisors are: 1"<<" "<<num<<" ";
  15.     for (root;1<root;root--)
  16. {
  17.         if(num%root==0)                                  //the loop and conditions
  18. {
  19.     dev=num/root;
  20.     cout<<root<<" "<<dev<<" ";
  21.  
  22. }
  23.        else
  24.     continue;                
  25. }
  26.     cout<<endl;
  27.     cin>>num;
  28. }
  29.     return 0;
  30. }
I wrote a code to determine a entered number as a prime number, ijust wanna know is it a good code or not? and also how can i relate this code with the code above with a loop to make a perfect code????
here is my code for primes!!!!!>>>>

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.    const int SENTINEL=-1;
  8.     cout<<"enter numbers between 1 and 1000 <enter -1 to exit> :";
  9.     cin>>num;                      //entered number by user
  10.     if(0<num && num<=range)               
  11.     {                                                  //LOOps and conditions
  12.     while(num!=SENTINEL)
  13.     {
  14.          for ( n=2; n < num; n++ )
  15.  
  16.             if (num%n==0 )
  17.                 break;                             //formula to determine a prime number
  18.             if ( n==num )        
  19.                 cout << num<<" is prime"<< endl;
  20.  
  21.     cin>>num;
  22.     }
  23.     }
  24.     else
  25.     cout<<"invalid Number, number should be between 1 and 1000";    
  26.     return 0;
  27. }
Oct 22 '07 #12
Laharl
849 Expert 512MB
It's not the most optimized code ever written, but does the job. For a quick optimization, think of what property all primes but 2 have and consider only numbers with that property.
Oct 22 '07 #13
can anybody give me a loop to connect the two code above?
i mean, user can input as many numbers as he/she desires and then get that these numbers are prime or perfect. and when they input a negative # or over 1000 it give error but not a input failure?
TANX
Oct 24 '07 #14
sicarie
4,677 Expert Mod 4TB
can anybody give me a loop to connect the two code above?
i mean, user can input as many numbers as he/she desires and then get that these numbers are prime or perfect. and when they input a negative # or over 1000 it give error but not a input failure?
TANX
Once again, given the site's policy on homework, what did you get when you tried?
Oct 24 '07 #15
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.    const int SENTINEL=-1;
  8.     cout<<"enter numbers between 1 and 1000 <enter -1 to exit> :";
  9.     cin>>num;                      //entered number by user
  10.     if(0<num && num<=range)               
  11.     {                                                  //LOOps and conditions
  12.     while(num!=SENTINEL)
  13.     {
  14.          for ( n=2; n < num; n++ )
  15.  
  16.             if (num%n==0 )
  17.                 break;                             //formula to determine a prime number
  18.             if ( n==num )        
  19.                 cout << num<<" is prime"<< endl;
  20.  
  21.     cin>>num;
  22.     }
  23.     }
  24.     else
  25.     cout<<"invalid Number, number should be between 1 and 1000";    
  26.     return 0;
  27. }
[/quote]

ok, considering above code!!!!!when i tried it, it determines ane number between 1 and 1000 as prime or not, correctly. but when a negative or a # greater than 1000 entered it gives an input failure and exites the program.
now i just want somebody to help me to find a loop that when an invalid # has been entered it gives an error but then loop to the first line (whithout input failure). Gives the user another chance to enter the number!!!!!

TANX
Oct 24 '07 #16
Ganon11
3,652 Expert 2GB
You will have to do exactly that: create a loop that continually asks the user for input until it is valid input.
Oct 24 '07 #17
for this code i added { while((num<0)||(1001<num)) } inorder to avoid input failure when user enteres a #r<0 or #>1001. BUt when i ran the program it just worked for the 1st #. for example if a user enters 1002 or negative # it worked great, but when the user enters 1002 2 1002 the program output was like

{{{{{{{ENTER #<enter -1 to stop>:1002 2 1002
ENTER #<enter -1 to stop>:devisors are: 1 2
devisors are: 1 1002 6 167 3 334 2 501
}}}}}}}}}}}}(it avoid the first 1002 but executed the 2nd one)
how can i correct it?????


Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. include<cmath>
  3. using namespace std;
  4. int main()
  5. {
  6. int num,root,dev;
  7. const int SENTINEL=-1;                                //constant and integers
  8. cout<<"ENTER #<enter -1 to stop>:";
  9.  
  10.  
  11. while((num<0)||(1001<num))
  12. {
  13.     cin>>num;                                   //# entered by user
  14.     while(num!=SENTINEL)    
  15. }             
  16. {
  17.     root=pow(num,0.5);                       //second root of that #
  18.     cout<<"devisors are: 1"<<" "<<num<<" ";
  19.     for (root;1<root;root--)
  20. {
  21.         if(num%root==0)                                  //the loop and conditions
  22. {
  23.     dev=num/root;
  24.     cout<<root<<" "<<dev<<" ";
  25.  
  26. }
  27.        else
  28.     continue;                
  29. }
  30.     cout<<endl;
  31.     cin>>num;
  32. }
  33.     return 0;
  34. }
Oct 24 '07 #18
You will have to do exactly that: create a loop that continually asks the user for input until it is valid input.
but how? i tried that on above post, but it did'nt work !!!!!Any idea Why????
Oct 24 '07 #19
tanx y all. I got it
i just added
while(num)
{
------
------
if(num==-100)
break;
else
{
----------------
----------
}

return 0;
Oct 26 '07 #20

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

Similar topics

7
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd...
9
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working...
6
by: zacariaz | last post by:
Once again i have tryed my lyck with classes and it seem that im getting the hang of it, but before i get too self confident, i would like to get an expert oppinion. I have made a working...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
2
by: QHorizon | last post by:
Hello, I'm new to Python (I've learned everything up to iterators so far) and fairly new to Programming. This would be my first real program: #Coordinate Geometry (The whole program is not...
5
by: cchris | last post by:
Hi, I'm a beginning C student and I'm working on a program that is supposed to display all the prime numbers between 50 and 100. I'm having trouble though. Can anyone help me out. This is what I have...
6
by: UofFprogrammer | last post by:
For a fun challenge, I am trying to create a C++ program that will determine if a number is prime. I am getting to problems when I reach a large values (over 9 or so digits). How can I increase the...
1
by: buomque | last post by:
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...
21
by: =?ISO-8859-1?Q?Rog=E9rio_Brito?= | last post by:
Hi, All. I'm just getting my feet wet on Python and, just for starters, I'm coding some elementary number theory algorithms (yes, I know that most of them are already implemented as modules, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.