Q:

Write a program in C++ to check whether a given number is Perfect or not

0

Write a program in C++ to check whether a given number is Perfect or not

Sample Output:

 Check whether a given number is a Perfect number:                                                   
------------------------------------------------------                                               
Input a number: 28                                                                                   
28 is a Perfect number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

# include <iostream>
# include <string>
using namespace std;
int main()
{
    int i=1, u=1, sum=0,n;
 cout << "\n\n Check whether a given number is a Perfect number:\n";
 cout << "------------------------------------------------------\n";
cout << "Input a number: ";
cin >> n;
   while(u<=n)
   {                              
     if(u<n)
     {
      if(n%u==0 )
      sum=sum+u;
     }                         
     u++;
   }                           
   if(sum==n)
   {
    cout<<n<<" is a Perfect number."<<"\n";
   }
   else
   {
     cout<<n<<" is not a Perfect number."<<"\n";  
   }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now