Check whether a given number is perfect number or not

Program to check whether a given number is a perfect number or not is discussed here.  A perfect number is a number which is equal to the sum of its proper positive divisors.

For example, 6 is a perfect number.
The divisors of 6 are 1, 2 and 3.
1 + 2 + 3 = 6.

This problem can be solved in two different ways.

Method 1: Starting from 1 to (num – 1), find all the divisors of the number. If the sum of divisors of the number is equal to the number, return true.

Method 2: Starting from 1 to (num^1/2), find all the divisors of the number. If the sum of divisors of the number is equal to the number, return true.

The second method is much more efficient than the first method. 

Algorithm to check whether a given number is a perfect number or not

  • Input the number.
  • Find all divisors of the number except the number itself.
  • If the sum of all divisors of the number is equal to the number, then return true. Else, return false.

Perfect number or not using method 1

// C program to check whether the given number is a perfect number or not

#include<stdio.h>

int main()
{
int i, num, sum = 0;

printf(“\nEnter a number : “);
scanf(“%d”, &num);

for(i=1; i<num; i++)
{
if(num%i == 0)
{
sum += i;
}
}

if(sum == num)
{
printf(“\n%d is a Perfect Number\n”, num);
}
else
{
printf(“\n%d is not a Perfect Number\n”, num);
}

return 0;
}

// C++ program to check whether the given number is a perfect number or not

#include <iostream>
using namespace std;

int main()
{
int i, num, sum = 0;

cout << “\nEnter a number : “;
cin >> num;

for(i=1; i<num; i++)
{
if(num%i == 0)
{
sum += i;
}
}

if(sum == num)
{
cout << “\n” << num << ” is a Perfect Number\n”;
}
else
{
cout << “\n” << num << ” is not a Perfect Number\n”;
}

return 0;
}

// Java program to check whether the given number is a perfect number or not

import java.util.*;
public class Main{

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a number : “);
int num = sc.nextInt();
int i, sum = 0;
for(i=1; i<num; i++)
{
if(num%i == 0)
{
sum += i;
}
}

if(sum == num)
{
System.out.print(num + ” is a Perfect Number\n”);
}
else
{
System.out.print(num + ” is not a Perfect Number\n”);
}
}
}

# Python program to check whether the given number is a perfect number or not

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(num,”is a perfect number”)
else:
print(num,”is not a perfect number”)

Time complexity: O(n)

Perfect number or not using method 2 (more efficient)

// C program to check whether a given number is a perfect number or not

#include<stdio.h>

int is_perfect_number(int n)
{
int sum = 1;

for (int i=2; i*i<=n; i++)
{
if (n%i==0)
{
if(i*i!=n)
sum = sum + i + n/i;
else
sum=sum+i;
}
}

if (sum == n && n != 1)
return 1;

return 0;
}

int main()
{
int n;
printf(“\nEnter a number : “);
scanf(“%d”,&n);
if (is_perfect_number(n))
printf(“%d is a perfect number\n”,n);
else
printf(“%d is not a perfect number\n”,n);

return 0;
}

// C++ program to check whether a given number is a perfect number or not

#include<iostream>
using namespace std;

int is_perfect_number(int n)
{
int sum = 1;

for (int i=2; i*i<=n; i++)
{
if (n%i==0)
{
if(i*i!=n)
sum = sum + i + n/i;
else
sum=sum+i;
}
}

if (sum == n && n != 1)
return 1;

return 0;
}

int main()
{
int n;
cout << “\nEnter a number : “;
cin >> n;
cout << endl;
if (is_perfect_number(n))
cout << n << ” is a perfect number\n”;
else
cout << n << ” is not a perfect number\n”;

return 0;
}

// Java program to check whether a given number is a perfect number or not

// Java program to check whether the given number is a perfect number or not

import java.util.*;
public class Main
{
public static boolean is_perfect_number(int n)
{
int sum = 1;

for (int i=2; i*i<=n; i++)
{
if (n%i==0)
{
if(i*i!=n)
sum = sum + i + n/i;
else
sum=sum+i;
}
}

if (sum == n && n != 1)
return true;

return false;
}

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a number : “);
int num = sc.nextInt();

if(is_perfect_number(num))
{
System.out.print(num + ” is a Perfect Number\n”);
}
else
{
System.out.print(num + ” is not a Perfect Number\n”);
}
}
}

# Python program to check whether a given number is a perfect number or not

def is_perfect_number(n):
sum = 1
i = 2
while i * i <= n:
if n % i == 0:
sum = sum + i + n/i
i += 1

return (True if sum == n and n!=1 else False)

num = int(input(“Enter a number : “))
if(is_perfect_number(num)):
print(num,”is a perfect number”)
else:
print(num,”is not a perfect number”)

Time complexity: O(n^1/2)

Output:

perfect number or not

perfect number or not