# Check whether a given number is a perfect number or not

### Example:-

6, 28, 496, 8128......

#### Sample Input/Output:-

Sample Input First: 6                         Sample Output First: It is a Perfect Number

Sample Input Second: 12                  Sample Output SecondIt is not a Perfect Number

#### Explanation:-

For output 1st: Factors of 6 are =1,2,3. sum of factors is=1+2+3=6=Given Number

For output 2nd: Factors of 12 are =1,2,3,4,6. sum of factors is=1+2+3+4+6=16≠Given Number

Data requirement:-

Input Data:- num

Output Data:-Sum

Program in C

Here is the source code of the C Program to Check whether a given number is a perfect number or not.

Code:

#include<stdio.h>
int main()
{
int num,i;
printf("Enter a number:");
scanf("%d",&num);
int sum=0;
for(i=1;i<num;i++)
{
if(num%i==0)
{
sum=sum+i;
}
}
if(sum==num)
printf("It is a Perfect Number");
else
printf("It is not a Perfect Number");
}

Input/Output:
Enter a number:6
It is a Perfect Number

Program in C++

Here is the source code of the C++ Program to Check whether a given number is a perfect number or not.

Code:

#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter a number:";
cin>>num;
int sum=0;
for(int i=1;i<num;i++)
{
if(num%i==0)
{
sum=sum+i;
}
}
if(sum==num)
cout<<"It is a Perfect Number";
else
cout<<"It is not a Perfect Number";
}

Input/Output:
Enter a number:12
It is not a Perfect Number

Program in Java

Here is the source code of the Java Program to Check whether a given number is a perfect number or not.

Code:

import java.util.Scanner;
public class PerfectNumberOrNot {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num,i;
System.out.println("Enter a number:");
num=cs.nextInt();
int sum=0;
for(i=1;i<num;i++)
{
if(num%i==0)
{
sum=sum+i;
}
}
if(sum==num)
System.out.println("It is a Perfect Number");
else
System.out.println("It is not a Perfect Number");
cs.close();
}
}

Input/Output:
Enter a number:
28
It is a Perfect Number

Program in Python

Here is the source code of the Python Program to Check whether a given number is a perfect number or not.

Code:

num=int(input("Enter a number:"))
sum=0
for i in range(1,num):
if(num%i==0):
sum=sum+i
if sum==num:
print("It is a Perfect Number")
else:
print("It is not a Perfect Number")

Input/Output:
Enter a number:36
It is not a Perfect Number