Perfect numbers are positive integers which have the special property that the sum of all their factors equals the number itself, e.g., 6 = 1+2+3. The C program finds and prints out all perfect numbers less than 1000.
#include <stdio.h> #include <stdlib.h> #define MAX 1000 int main() { long n, sum, j ,k, half; clrscr(); printf("\n PERFECT NUMBERS\n"); for(j=1;j<MAX;j++) { half= (j+1)/2; sum = 0; for(k=1;k<=half; k++) { if(j%k==0) sum+= k; } if(sum==j) { printf("\r %7ld is a perfect number.\n", j); } } return 0; }