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;

}

Output:

Bikash Panda
Catch Me On

Bikash Panda

Blogger / Embedded System Developer at WeArGenius
A techie, tinkerer and tech lover, who loves to blog and feels everyone can learn tech provided they have the right attitude towards learning and passion. By profession, I am an IOT developer working in Smart Home/ Smart Grid domain.
Bikash Panda
Catch Me On

Latest posts by Bikash Panda (see all)

Bikash Panda

A techie, tinkerer and tech lover, who loves to blog and feels everyone can learn tech provided they have the right attitude towards learning and passion. By profession, I am an IOT developer working in Smart Home/ Smart Grid domain.