fork download
  1. // C++ program to check if a given number is perfect
  2. // or not
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. // Returns true if n is perfect
  7. bool isPerfect(long long int n)
  8. {
  9. // To store sum of divisors
  10. long long int sum = 1;
  11.  
  12. // Find all divisors and add them
  13. for (long long int i=2; i*i<=n; i++)
  14. {
  15. if (n%i==0)
  16. {
  17. if(i*i!=n)
  18. sum = sum + i + n/i;
  19. else
  20. sum=sum+i;
  21. }
  22. }
  23. // If sum of divisors is equal to
  24. // n, then n is a perfect number
  25. if (sum == n && n != 1)
  26. return true;
  27.  
  28. return false;
  29. }
  30.  
  31. // Driver program
  32. int main()
  33. {
  34. int n; cin>>n;
  35. if (isPerfect(n))
  36. cout <<"Perfect"<<endl;
  37. else cout<<"Not Perfect"<<endl;
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 5548KB
stdin
6
stdout
Perfect