C Program to Check Perfect Number or Not
Perfect number in C
Basically, a perfect number is a positive number that is equal to the sum of all its divisors(excluding itself) excluding itself.
We have to find all divisors of that number and find their sum if the sum of divisors is equal to the number. We will look at different ways of coding Perfect Number in C.
Ex:- Take a number: 6 6 is a perfect number as 1 + 2 + 3 = 6. Ex:- Take a number: 28 28 is a perfect number as 1 + 2 + 4 + 7 + 14 = 28
Methods to find
- Method 1: Using For Loop, Linear traversal between [1, num -1] to find factors
- Method 2: Using While Loop, Linear traversal between [1, num – 1] to find factors
- Method 3: Linear traversal between [1, num/2] to find factors
- Method 4: Using Function and Linear traversal between [1, num/2] to find factors
- Method 5: Uses recursion in C
- Method 6: Linear traversal between [1, √num] but using a pair-based approach
Method 1 (For Loop)
For a user input num
- Using loop in the iteration of (i) traverse from [1, num-1]
- Add all (i) that divide num perfectly (num % i == 0)
Method 1 C Program:-
Lets have a look at the program below –
#include<stdio.h> int main () { int num = 28, sum = 0; // iteratively check for all numbers in range [1, 27] for(int i = 1; i < num; i++){ // check if i is a divisor, if yes then add to sum if(num % i == 0) sum = sum + i; } if(sum == num) printf("%d is a perfect number",num); else printf("%d is not a perfect number",num); } // Time complexity: O(N) // Space complexity: O(1)
Output:-
28 is a perfect number
Method 2 (While Loop)
Uses the same logic as method 1.1 but rather than a for loop uses a while loop.
Method 2 C Program:-
Lets have a look at the program for perfect number in C below –>#include<stdio.h> int main () { int num = 28, sum = 0, i = 1; // iteratively check for all numbers if they are divisors while(i < num) { // check if i is a divisor, if yes then add to sum if(num % i == 0) sum = sum + i; i++; } if(sum == num) printf("%d is a perfect number",num); else printf("%d is not a perfect number",num); } // Time complexity: O(N) // Space complexity: O(1)
Output:-
28 is a perfect number
Method 3 In Range [1, num/2]
This method uses optimization that all divisors of the number(excluding itself) can be found in the range [1, num/2]
Method 3 C Program:-
Lets have a look at the program for perfect number in C below –
#include <stdio.h> int main () { int num = 28, sum = 0, i = 1; // all divisors of the numbers (excluding the number itself) // can be found before num/2 // note we will need to use '=' sign as for even numbers // like 28, half of the number i.e 14 will also be the divisor while(i <= num/2) { // check if i is a divisor, if yes then add to sum if(num % i == 0) sum = sum + i; i++; } if(sum == num) printf("%d is a perfect number",num); else printf("%d is not a perfect number",num); } // Time complexity: O(N) // Space complexity: O(1)
Output:-
28 is a perfect number
Method 4 C Program:-
Lets have a look at the program for perfect number in C below –
#include <stdio.h> int isPerfect(int num) { int sum = 0; // all divisors of num(excluding itself) can be found before num/2 // remember put = sign as for even numbers like 28 // half of it i.e. 14 would be divisor too e for (int i = 1; i <= num/2; i++){ if (num % i == 0) sum = sum + i; } if (sum == num) return 1; return 0; } int main () { int num = 28, sum = 0, i = 1; if(isPerfect(num)) printf("%d is a perfect number",num); else printf("%d is not a perfect number",num); } // Time complexity: O(N) // Space complexity: O(1)
Method 4 In Range [1, num/2] using a function
In this method, we use a function.
This method also uses optimization that all divisors of the number(excluding itself) can be found in the range [1, num/2]
Output:-
28 is a perfect number
Method 5
This method uses recursion in C
Method 5 C Program:-
Lets have a look at the program for perfect number in C below –
#include <stdio.h> static int sum = 0; int getSumDivisors(long num, int i) { // since, all factors can be found will num/2 // we will only check in this range if(i <= num/2) { if(num % i ==0) sum = sum + i; i++; // recursively call isPerfect getSumDivisors(num, i); } //returns the sum // when i becomes > num/2 return sum; } int main () { int num = 28; if(getSumDivisors(num, 1) == num) printf("%d is a perfect number",num); else printf("%d is not a perfect number",num); return 0; } // Time complexity: O(N) // Space complexity: O(1) // Auxiliary Space complexity: O(N) due to function call stack
Output:-
28 is a perfect number
Method 6
This method uses observations that all factors come in pairs.
Example 1 : 100
(1,100), (2, 50), (4, 25), (5, 20), (10, 100)
Example 2 : 28
(1, 28), (2, 14), (4, 7)
Note : We will need to ignore pair of 1. As it will be the number itself.
Since we will find all pairs before √num (n = sqrt(n) * sqrt(n))
Example: For 28, all pairs can be found before √28 = 5.2
Method 6 C Program:-
#include <stdio.h> #include <math.h> int main () { int num = 28, sum = 0; for(int i = 1; i < sqrt(num); i++) { if (num % i == 0) { // For num : (1, num) will always be pair of divisor // acc to def., we must ignore adding num itself as divisor // when calculating for perfect number if(i == 1) sum = sum + i; // Example For 100 (10,10) will be one pair // But, we should add value to the sum just once else if(i == num/i) sum = sum + i; // add both pairs as divisors // For any divisor i, num/i will also be a divisor else sum = sum + i + num/i; } } if(sum == num) printf("%d is a perfect number",num); else printf("%d is not a perfect number",num); } // Time complexity: O(sqrt(N)) // Space complexity: O(1)
Output:-
28 is a perfect number
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
#include
int main()
{
int i,num,sum=0,temp;
printf(“Enter the number :\n”);
scanf(“%d”,&num);
temp=num;
for(i=1;i<num;i++)
{
if(num%i==0)
{
sum=sum+i;
}
}
if(sum==temp)
printf("It is a Perfect Number");
else
printf("It is not a Perfect Number");
return 0;
}
Perfect number in Python:
num = int(input(“Enter a number : “))
factors = []
for i in range(1, num+1):
if num % i == 0:
factors.append(i)
print(factors)
l = len(factors)
f1 = factors[:l-1]
print(f1)
perfect_num = 0
for i in f1:
perfect_num = perfect_num + i
print(perfect_num)
if num == perfect_num:
print(“It is a perfect number”)
else:
print(“It is not a perfect number”)
There is no error in the code but at the time of execution its not displaying the output
Alternate code using for loop
#include
int main()
{
int n,i,perfect=0;
printf(“enter a number”);
scanf(“%d”,&n);
for(i=1;i<n;i++)
{
if(n%i==0)
{
perfect=perfect+i;
}
}
if(perfect==n)
{
printf(" %d is perfect number", perfect);
}
else
{
printf(" %d is not perfect number",n);
}
return 0;
}
#python3
def perfect(num):
return sum([i for i in range(1,num) if num%i==0])==num
n=int(input())
print(perfect(n))
perfect number using python:
n = int(input(“Enter any number: “))
sum1 = 0
for i in range(1, n):
if(n % i == 0):
sum1 = sum1 + i
if (sum1 == n):
print(“The number is a Perfect number!”)
else:
print(“The number is not a Perfect number!”)
The given code is incorrect
Hey Akshobhya, can you please tell us, where did you find error in the code