Oh, you did post the code afterwards. Still, you need to format it properly, e.g.,
Code:
#include<stdio.h>
int main(void)
{
long count, i, j, n, k;
long sum;
printf(" Enter how many perfect no to print\n");
scanf("%d", &n);
count = 1;
sum = 0;
printf("%d\t", 1);
i = 2;
while (count <= n - 1)
{
sum = 0;
for (j = 1; j <= i / 2; j++)
{
k = i % j;
if (k == 0)
sum = sum + j;
}
if (sum == i)
{
printf("%d\t", sum);
count = count + 1;
}
i++;
}
}
I have taken the liberty of explicitly declaring the return type of main to be int.
Originally Posted by
Shashank Mishra
when i seek 6th one which is 33550336, it just does not print anything but remain stuck at previous perfect no which is 8198.
I waited for hours but still nothing.
Larger composite numbers tend to have more divisors, and then you're testing more and more numbers, so it is probably just a case of more time being needed (or a faster algorithm, if feasible). You can always print some output every say, 10000 integers to convince yourself that your program is still running.
Originally Posted by
MOS-6581
You never modify n after that line so no wonder it becomes an infinite loop.
No, it is not an infinite loop, assuming that there are enough perfect numbers to satisfy the request. Refer to: