i have recently started to learn c programming and im trying to improve my skill.i have some question about perfect numbers i searched forum but results were about C++.

question is this code finds perfect numbers between 1 to 1000000. but its not working after 8128 which is perfect number.

#include<stdio.h>
#include<conio.h>
int main (void)
{
int i,j,sum;

for(i=1;i<=1000000;i++)
{
j=1;
sum=0;
while(j<i)
{
if(i%j==0)
sum=sum+j;
j++;
}
if(sum==i)
printf("%d\n",i);
}
getch();
}

Recommended Answers

All 3 Replies

It could be 'appearing' to hang because the algorithm is very inefficient and its taking a long time to calculate the bigger perfect numbers. Let it run for a few hours.

Perfect numbers are few and far between the next perfect number after 8128 is 33550336 which is rather greater than 1000000 so your program can not find it.

It would appear that your program may be working.

Thanks for helping.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.