Check whether a number is perfect or not
The below program takes a integer as input and checks whether the input is a perfect number or not. Perfect number is a positive integer which is the sum of its proper positive divisor which means the sum of its positive divisors excluding the number itself.
Ex:
6 is a perfect number as it is sum of proper positive number.
6=1+2+3
28 is a perfect number as it is sum of proper positive number.
28=1+2+4+7+14
Code:
#include<iostream> using namespace std; int main() { int n,r=0; cout<<"Enter a number : "; cin>>n; for(int i=1;i<n;i++) { if(n%i==0) r=r+i; } if(r==n) cout<<n<<" is a perfect number."; else cout<<n<<" is not a perfect number."; return 0; }
Comments
Post a Comment