将大于1小于17的非素数存入一维数组

将大于1小于17的非素数存入一维数组
2024-11-07 17:55:33
推荐回答(1个)
回答(1):

#include

void fun( int m, int *k, int xx[] ) {
int i,j,t = 0;
*k = 0;
for (i = 2; i < m; i++) {
for (j = 2; j * j <= i; j++) {
if (i%j == 0) {
xx[t++] = i;
(*k)++;
break;
}
}
}
}

int main() {
int m, n, zz[100];
void NONO ();
printf( "\nPlease enter an integer number between 10 and 100: " );
scanf("%d", &n );
fun( n, &m, zz );
printf( "\n\nThere are %d non-prime numbers less than %d:", m, n );
for( n = 0; n < m; n++ )
printf( "\n %4d", zz[n] );
NONO();
return 0;
}

void NONO() {
/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */
int m, n, zz[100];
FILE *rf, *wf ;
rf = fopen("F:\\k01\\24010001\\in.dat","r") ;
wf = fopen("F:\\k01\\24010001\\out.dat","w") ;
fscanf( rf, "%d", &n );
fun( n, &m, zz );
fprintf(wf, "%d\n%d\n", m, n );
for( n = 0; n < m; n++ )
fprintf(wf, "%d\n", zz[n] );
fclose(rf) ;
fclose(wf) ;
}