3

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

4

2 回答 2

1

您的程序第一次通过循环无限循环。

当 i = 0 时,此循环永远不会结束:

for(j=2;(i*j)<n;j++)
于 2013-09-18T04:46:44.997 回答
1

您的printf调用可能正在被缓冲,这意味着在缓冲区填满或遇到换行符之前,它实际上可能不会被打印。

尝试在字符串末尾添加换行符或fprintf(stderr, ...)改为调用(未缓冲)。

于 2013-09-18T04:36:26.000 回答