Tuesday, May 24, 2016

Perfect Numbers Code in C++

#include<iostream>
#include<cmath>

using namespace std;

int isPerfect(int n)
{
 int i,sum;
 sum=0;
 for(i=1; i<n; i++)
 {
  if(n%i==0) sum+=i;
 }
 
 return sum;
}

int main()
{
 int n;
 cin>>n;
 if(isPerfect(n)==n) cout<<"This is perfect"<<endl;
 else cout<<"Not perfect"<<endl;
 
 cout<<"Some perfect nums are :D \n";
 for(int i=2; i<100; i++)
 {
  if(isPerfect(i)==i) cout<<i<<" ";
 }

 return 0;
}

No comments:

Post a Comment

Thank you for commenting. Please wait for response :)