Print all Perfect numbers between 1 to n with C.
#include <stdio.h>
int main()
{
int i, j, end, sum;
printf("Enter upper limit: ");
scanf("%d", &end);
printf("All Perfect numbers between 1 to %d:\n", end);
for (i = 1; i <= end; i++)
{
sum = 0;
for (j = 1; j < i; j++)
{
if (i % j == 0)
{
sum += j;
}
}
if (sum == i)
{
printf("%d ", i);
}
}
return 0;
}
Output:-
Enter upper limit : 50
All Perfect numbers between 1 to 50 : 6 28
0 Comments