程序设计题。
编写函数int fun(int lim, int aa[MAX]),其功能是求出小于或等于lim的所有素数并放在aa数组中,并返回求出的素数的个数。
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int fun(int lim, int aa[MAX])
{int p=0;for (int i=2; i<=lim; i++) {int flg = 1;for (int j=2; i>2 && j<=sqrt(i); j++)if (i%j == 0) {flg = 0;break;}if (flg) {aa[p++] = i;}}return p;
}
void main()
{FILE *wf;int limit, i, sum;int aa[MAX];system("CLS");printf("输入一个整数:");scanf("%d", &limit);sum=fun(limit, aa);for (i=0; i<sum; i++){if (i%10==0 && i!=0)printf("\n");printf("%5d", aa[i]);}wf=fopen("out.dat", "w");sum=fun(15, aa);for (i=0; i<sum; i++){if (i%10==0 && i!=0)fprintf(wf, "\n");fprintf(wf, "%5d", aa[i]);}fclose(wf);
}