Perfect Number or Not in C Program
ADVERTISEMENTS
Perfect number or not in c program.
In this article, you will learn how to check whether a given number is a perfect number.
What is the perfect number?
A perfect number is a positive integer equal to the sum of its positive divisors, excluding the number itself.
Example-1 A Perfect Number
Input: 6
6 / 1 = zero reminder
6 / 2 = zero reminder
6 / 3 = zero reminder
Divisors
6 / 1 = zero reminder
6 / 2 = zero reminder
6 / 3 = zero reminder
Divisors
(1 +2 + 3) == 6
Input Number
There are three ways to solve this problem:
- Using while loop
- Using do-while loop
- Using for loop
You should know about the following topics in C programming to understand this program:
- If-else statement
- While loop in C
- Do-while loop in C
- For loop in C
1 - Perfect Number or Not in C using While loop
// Perfect Number or Not in C using While loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, result = 0;
// Step-1 Take input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Check input number should be positive
if (num > 0)
{
// Step-3 Use a loop to find the perfect number
int i = 1;
while (i <= num)
{
if (num % i == 0)
result += i;
i++;
}
// Step-3 Check the Result(sum of factors) == 2*Input Number
if (result == 2 * num)
printf("%d IS A PERFECT NUMBER.\n", num);
else
printf("%d IS NOT A PERFECT NUMBER.\n", num);
}
else
printf("Enter a positive number!\n");
return 0;
}
Output
INPUT A NUMBER: 28
28 IS A PERFECT NUMBER.
2 - Perfect Number or Not in C using Do-while loop
// Perfect Number or Not in C using Do-while loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, result = 0;
// Step-1 Take input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Check input number should be positive
if (num > 0)
{
// Step-3 Use a loop to find the perfect number
int i = 1;
do
{
if (num % i == 0)
result += i;
i++;
} while (i <= num);
// Step-3 Check the Result(sum of factors) == 2*Input Number
if (result == 2 * num)
printf("%d IS A PERFECT NUMBER.\n", num);
else
printf("%d IS NOT A PERFECT NUMBER.\n", num);
}
else
printf("Enter a positive number!\n");
return 0;
}
Output
INPUT A NUMBER: 30
30 IS NOT A PERFECT NUMBER.
3 - Perfect Number or Not in C using For loop
// Perfect Number or Not in C using For loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, result = 0;
// Step-1 Take input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Check input number should be positive
if (num > 0)
{
// Step-3 Use a loop to find the perfect number
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
result += i;
}
// Step-3 Check the Result(sum of factors) == 2*Input Number
if (result == 2 * num)
printf("%d IS A PERFECT NUMBER.\n", num);
else
printf("%d IS NOT A PERFECT NUMBER.\n", num);
}
else
printf("Enter a positive number!\n");
return 0;
}
Output
INPUT A NUMBER: 496
496 IS A PERFECT NUMBER.