C Program to Display all perfect numbers below 500 - IProgramX

Q. Display all perfect numbers below 500 [A perfect number is a number, such that the sum of its factors is equal to the number itself]. Example: 6 (1 + 2 + 3), 28 (1+2+4+7+14)  


Program

#include <stdio.h>
void main()
{
    int i, j, sum;
    printf("All Perfect numbers between 1 to 500 :\n");
    for(i=1; i<=500; i++)
    {
        sum = 0;
        for(j=1; j<i; j++)
        {
            if(i % j == 0)
            {
                sum += j;
            }
        }
        if(sum == i)
        {
            printf("%d, ", i);
        }
    }
}

Output:

All Perfect numbers between 1 to 500 :
6, 28, 496,

Post a Comment

0 Comments