Explore
ProGrad Programs
About Us

Edit
Reply




Edit

Check whether a given number is perfect number or not | FACE Prep

Published on 10 Mar 2020

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
C++
Java
Python 3

Output
Input- Enter a number:35 Output- 35 is not a Perfect Number


Time complexity: O(n)


Perfect number or not using method 2 (more efficient)


C
C++
Java
Python 3

Output
Input- Enter a number:6 Output- 6 is a perfect number


Time complexity: O(n^1/2)


Recommended Programs










If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in
Explore 'c'
Articles