English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i have to create this program in c++ as a project and i want to know how to check whether a number is perfect or not and also to check whether a number is prime or not.

2006-06-09 01:50:24 · 4 answers · asked by hellmaster_aa 2 in Education & Reference Homework Help

4 answers

to check if it is perfet or not:
cin>>number;
int sum=0;
for (int i=1;i if (number%i==0)
sum+=i;
if (sum==number)
cout<<"Number is perfect";



and to check if it is prime:

cin>>number;
int flag=0;
for (int i=2;i if (number%i==0)
flag=1;
if (flag==0)
cout<<"number is prime";
else
cout<<"Number isnt prime";

2006-06-09 01:57:22 · answer #1 · answered by Anonymous · 0 0

In mathematics, a perfect number is defined as an integer which is the sum of its proper positive divisors, that is, the sum of the positive divisors not including the number. Equivalently, a perfect number is a number that is half the sum of all of its positive divisors, or σ(n) = 2 n.

Six (6) is the first perfect number, because 1, 2 and 3 are its proper positive divisors and 1 + 2 + 3 = 6. The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. The next perfect numbers are 496 and 8128


void main()
{
int sum =0;
int i,n;
cout <<"\n enter the no";
cin>> n;
for( i =1; i< =n/2; i++)
{
if (( n % i) ==0)
sum =sum + i;
} /* end of if */

} /* end of for*/

if( sum == n)
cout<<"\n the number is pefect";




} /* end of main*/

2006-06-09 09:01:04 · answer #2 · answered by Rahul Agarwal 2 · 0 0

to check if it is perfect (i'm assuming you are referring to if it is a perfect square), you want to assign a variable to be the sqrt of the input variable. Make sure the assigned variable is a real number, you don't want it to be an integer as it will yield a false answer. You want to then check if the sqrt of the input variable is a whole number.

2006-06-09 08:55:13 · answer #3 · answered by cnuswte 4 · 0 0

http://www.phon.ucl.ac.uk/courses/spsci/abc/perfect.cpp

2006-06-09 08:59:18 · answer #4 · answered by CapeMill 1 · 0 0

fedest.com, questions and answers