Perfect number
in mathematics, perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For instance, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number. Let's see the following example
QUESTION:- Write a c program to check whether a given number is a perfect number or not.
Test Data :
Input the number: 56
Expected Output :
The positive divisor: 1 2 4 7 8 14 28
The sum of the divisor is: 64
So, the number is not perfect.
#include <stdio.h>
#include <conio.h>
void main()
{
int no, sum = 0;
printf("enter a value: ");
scanf("%d", &no);
for (int i = 1; i <= (no / 2); i++)
{
if (no % i == 0)
{
sum = sum + i;
}
}
if (sum == no)
{
printf("%d is perfect number with sum %d ", no, sum);
}
else
{
printf("%d is not a perfect number with sum %d ", no, sum);
}
}
enter a value: 10
10 is not a perfect number with a sum of 8
Follow our page for regular updates.
0 Comments