C++ program using function to check whether the entered number is a perfect number or not?

/*
What is a Pefect Number?
Perfect number is a number, where the sum of the divisors is equal to that number.
e.g.
6:Divisors of 6 is 1, 2 and 3, sum of 1, 2 and 3 is 6
28:Divisors of 28 is 1, 2, 4, 7 and 14, sum of 1, 2, 4, 7 and 14 is 28
*/

#include<iostream.h>
#include<conio.h>
int Sum_Divisors(int);
void main()
{
int n;
clrscr();
cout<<"Enter any number check whether the number is perfect or not? ";
cin>>n;
if(n==Sum_Divisors(n))
cout<<n<<" is a perfect number...\n";
else
cout<<n<<" is not a perfect number...\n";
getch();
}

int Sum_Divisors(int n)
{
int sum=0;
for(int i=1;i<n;i++)
{
if(n%i==0)
sum+=i;
}
return sum;
}
Powered by Blogger.