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;
}
Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.
Related Articles
Basic Courses
Advance Courses