C++ Program to Find Perfect Number

This program will find whether a given number is perfect or not. A perfect number is that in which we get the same result if we multiply the digits of the number or we add the digits of the number. For example: 321.
Here 3+2+1=3*2*1=6. It means 321 is a perfect number.

#include
#include
void main()
{
int n,r,m=1,s=0;
clrscr();
cout<<"Enter value of n:";
cin>>n;
while(n>0)
{
r=n%10;
n=n/10;
m=m*r;
s=s+r;
}
if(s==m)
cout<<"nNumber is a perfect number.";
else
cout<<"nNumber is not a perfect number.";
getch();
}

Output:
Enter value of n:123
Number is a perfect number.

Add Comment