/* Program Print Prime Numbers */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
int *primes=NULL;
int trial=0;
bool found=false;
size_t total=0;
size_t count=0;
printf("\nHow many primes would you like?\n");
scanf("%d",&total);
total=(total<4?4:total);
printf("%d",total);
primes= (int*)malloc(total*sizeof(int));
if(primes==NULL)
{
printf("\nNot enough memory\n");
return 1;
}
*primes=2;
*(primes+1)=3;
*(primes+2)=5;
count=3;
trial=5;
while(count<total);
{
trial+=2;
for(size_t i=0;i<count;i++)
if(!(found=(trial % *(primes+i))))
if(found)
*(primes+count++)=trial;
}
for(size_t i=0;i<total;i++)
{
if(!(i%5))
printf("\n");
printf("%d",*(primes+i));
}
printf("\n");
return 0;
}
这是我用来学习 C 编程的书中的 C 程序。这个程序不起作用。当它应该输入“total”变量时,程序继续输入值。你想要多少个素数?4 5 10...像这样