C program to find perfect number
In this post, you will learn how to check the perfect number using the C programming language.
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. The perfect number program is generally asked in C coding tests and interviews.
The sum of divisors of a number, excluding the number itself, is called its aliquot sum, so a perfect number is one that is equal to its aliquot sum. For example, 6 has divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a perfect number. 6 is the first perfect number.
Perfect Number | Sum of its Divisors |
6 | 1+2+3 |
28 | 1+2+4+7+14 |
496 | 1+2+4+8+16+31+62+124+248 |
8,128 | 1+2+4+8+16+32+64+127+254+508+1,016+2,032+4,064 |
Here, we have mentioned three different ways to find the perfect number in C.
Perfect Number Program in C using For loop
The given C program asks the user to enter any number. We store it in a variable num. Next, this will check whether the number is a perfect number or not using the for loop. Next, using an if statement, we check if the number divided by i gives the remainder as 0, which is basically the proper divisor of the integer. If the sum of the proper divisors of the number is equal to the original number, the number is a perfect number.
/*C program to check the Perfect number*/
#include<stdio.h>
void main()
{
// declaring the variables
int num, rem, n_sum = 0, i;
// getting input from the user
printf("Enter a number: \n");
scanf("%d", &num);
// find all divisors and add them
for(i = 1; i < num; i++)
{
rem = num % i;
if (rem == 0)
{
n_sum = n_sum + i;
}
}
if(n_sum == num)
printf(" %d is a perfect number.",num);
else
printf("\n %d is not a perfect number.",num );
}
Output of the above code:
Enter a number:
6
6 is a perfect number.
Enter a number:
160
160 is not a perfect number.
Perfect Number Program in C using While loop
In the given C program, we ask the user to enter any number. Next, we initialise the n_sum to 0 and the i to 1. We iterate the loop till the value of i is less than the entered number. Within the loop, if a proper divisor is found, we add it to the n_sum. Finally, we compare the sum of divisors with the number and display the required result.
/* C Program to find the perfect number using while loop*/
#include<stdio.h>
void main()
{
int i = 1, num, n_sum = 0;
printf("Enter any number: \n");
scanf("%d", &num);
while(i < num )
{
if(num % i == 0)
n_sum = n_sum + i;
i++;
}
if(n_sum == num)
printf("\n %d is Perfect Number.", num);
else
printf("\n %d is not a Perfect Number.", num);
}
Output of the above code:
Enter any number:
496
496 is Perfect Number.
Enter any number:
112
112 is not a Perfect Number.
C program to find perfect number between 1 and 1000
The given C program for a perfect number allows the user to enter a minimum and maximum value. We use the for loop to iterate between the given minimum and maximum variables and check whether that number is perfect or not.
/* C Program to find the perfect number between 1 to 1000 */
#include <stdio.h>
int main()
{
int num, n_sum, i, min, max;
// getting inputs from the user
printf("\nPlease Enter the Minimum & Maximum Values: \n");
scanf("%d %d", &min, &max);
printf("Perfect Numbers Between %d and %d are:\n", min, max);
for(num=min; num<=max; num++)
{
for(i = 1, n_sum =0 ; i < num ; i++)
{
if(num % i == 0)
n_sum = n_sum + i ;
}
if (n_sum == num)
printf(" %d ", num) ;
}
return 0;
}
Output of the above code:
Please Enter the Minimum & Maximum Values:
1 1000
Perfect Numbers Between 1 and 1000 are:
6 28 496
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative