Monday, December 3, 2012

Find whether a given number is perfect or not using c++

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<iostream>
using namespace std;
int  main()
{
 int n,r,m=1,s=0;
 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.";
 system("pause");
 return 0;
}
Output:
Enter value of n:123 Number is a perfect number.

No comments:

Post a Comment