学习目标:
编写一个函数判别某一数是否为素数,若是,返回值为1;否则,返回值为0。
学习内容:
1.了解素数的定义:指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。
2.掌握并熟练运用for循环语句。
代码:
#include <stdio.h>
#include <stdlib.h>
int judge(int n) {
int i = 0;
for (i = 2; i < n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int a = 0;
int b = 0;
scanf("%d", &a);
b = judge(a); //调用自定义函数judge(),并将返回值赋给b
printf("%d", b);
return 0;
}