C++ program using function to print the list of perfect numbers upto N.

/*
What is a Perfect 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>
long Sum_Divisors(long);
void main()
{
long int n, i;
clrscr();
cout<<"Enter any number check whether the number is perfect or not? ";
cin>>n;
for(i=1;i<=n;i++)
{
if(i==Sum_Divisors(i))
cout<<i<<" \t";
}
getch();
}

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