Perfect number in c++




Perfect number code in c++


#include<iostream>
#include<iomanip>
int main(){
  int n,i=1,sum=0;

  cout<<"Enter a number: ";
  scanf("%d",&n);

  while(i<n){
         if(n%i==0)
               sum=sum+i;
              i++;
  }
  if(sum==n)
         cout << i  <<  " is a perfect number";
  else
         cout << i << " is not a perfect number";

  return 0;
}


Programs of c++


5 comments:

Cathryn Van Der Linden said...

Thank you for this help! It was simple and easy to understand

Amit Bhardwaj said...

scanf in c++?

Amit Bhardwaj said...

scanf in c++?

Kamlesh Bheri said...

cin>>n;

alex trinidad said...

I am having a hard time making this code in c, please help. Basically a triangle pattern with numbers and corresponding numbers of asterisk.
1
*
2. 3
** ***

4. 5 6
**** ***** ******