需求:
写一个函数,输入一个数n,输出1到n之间所有的质数。(注:质数又称素数。一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数。)
输入样例:
10
输出样例:
2 3 5 7
代码实现:
#include <stdio.h>
int ifPrimes(int i)
{for(int j = 2; j < i; j++){if(i % j == 0 ){return 0;} }return 1;
}void printfPrime(int num)
{if(num <= 1){printf("please input num (num>1) !");}int i;for(i = 2; i <= num; i++){if(ifPrimes(i)){printf("%d ",i);}}}int main()
{int num = 0;printf("please input num:\n");scanf("%d",&num);printf("num have Primes:\n");printfPrime(num);return 0;
}
打印: