Write a function that check if a number is a Perfect number.
In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.
For example 28 = 1 + 2 + 4 + 7 + 14 is a perfect number.
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
int isPerfect(int nb)
{
int i, sum, n;
sum = 0;
n = nb;
for (i = 1; i<n; i++)
if (n%i == 0)
sum += i;
return (nb == sum);
}
void main()
{
int a;
printf("Enter an integer: ");
scanf("%d", &a);
if (isPerfect(a))
printf("%d is Perfect.\n",a);
else
printf("%d is not Perfect.\n", a);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Merge 2 sorted arrays using an iterative function