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:

Unknown said...

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

Unknown said...

scanf in c++?

Unknown said...

scanf in c++?

Unknown said...

cin>>n;

Unknown 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
**** ***** ******