Home »
C programs »
C number manipulation programs
C program to check whether number is Perfect Number or not
What is Perfect Number?
A perfect number is a positive integer number which is equals to the sum of it's proper positive divisors excluding the number itself. For example 6 is a perfect number because it's proper divisors are 1, 2, 3 and it's sum is equals to 6.
Checking whether number is Perfect Number or not
To check perfect number we have to find all divisors of that number and find their sum, if sum of divisors is equal to number it means number is Perfect Number.
Check Perfect Number using C program
/*C program to check nunber is perfect or not.*/
#include <stdio.h>
int main()
{
int num, loop;
int sum;
printf("Enter an integer number: ");
scanf("%d", &num);
sum = 0;
for (loop = 1; loop < num; loop++) {
if (num % loop == 0)
sum += loop;
}
if (sum == num)
printf("%d is a perfect number.", num);
else
printf("%d is not a perfect number.", num);
return 0;
}
Counting perfect number using user-defined function
/*C program to check nunber is perfect or not.*/
#include <stdio.h>
/*function to check perfect number or not*/
int isPerfect(int num)
{
int loop, sum = 0;
for (loop = 1; loop < num; loop++) {
if (num % loop == 0)
sum += loop;
}
if (sum == num)
return 1; /*Perfect Number*/
else
return 0; /*Not Perfect Number*/
}
int main()
{
int num, loop;
int sum;
printf("Enter an integer number: ");
scanf("%d", &num);
if (isPerfect(num))
printf("%d is a perfect number.", num);
else
printf("%d is not a perfect number.", num);
return 0;
}
Output
First Run:
Enter an integer number: 6
6 is a perfect number.
Second Run:
Enter an integer number: 496
496 is a perfect number.
Third Run:
Enter an integer number: 695
695 is not a perfect number.
C Number Manipulation Programs »