Here is a program to find prime numbers using sieve of Eratosthenes. The program is compiling but on execution, it becomes non responsive.The print statement itself is not executed. Can I know where I have gone wrong?
#include<stdio.h>
int main()
{
printf("Enter the range");
int n,i;
scanf("%d",&n);
int j;
int a[--n];
for(i=0;i<n;i++)
a[i]=i+2;
for(i=0;i<n;i++)
if(a[i])
{
printf("%d",a[i]);
for(j=2;(i*j)<n;j++)
a[i*j]=0;
}
return 0;
}
Thanks
Your program is infinite looping the first time through the loop.
When i = 0 this loop never ends:
for(j=2;(i*j)<n;j++)
Your printf call might be being buffered which means it might not actually be printed until the buffer fills up or a newline is encountered.
Try adding a newline to the end of your string or call fprintf(stderr, ...) instead (which isn't buffered).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With