Write a C function named isPrime that checks whether a given number is a prime or not. Include the fuction isPrime in a working program to display the first 50 prime numbers in five lines, each of which contains ten numbers. Make sure the function isPrime is called from the main method, and correctly returns a value to the main method.
时间: 2024-03-17 21:42:41 浏览: 55
my c code check a num whether it is a int
Sure, here is a C program that includes the `isPrime` function to display the first 50 prime numbers:
```
#include <stdio.h>
int isPrime(int num);
int main() {
int count = 0;
int num = 2;
while (count < 50) {
if (isPrime(num)) {
printf("%d ", num);
count++;
if (count % 10 == 0) {
printf("\n");
}
}
num++;
}
return 0;
}
int isPrime(int num) {
int i;
if (num < 2) {
return 0;
}
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}
```
The `isPrime` function takes an integer as input and returns 1 if the number is prime, and 0 otherwise. The `main` function uses a while loop to generate the first 50 prime numbers and prints them out in five lines, each containing ten numbers. The `isPrime` function is called from the `main` function to check whether each number is prime.
阅读全文