Search⌘ K
AI Features

Solution: Is the Number Prime

Explore how to write a C function that checks whether a given number is prime. Understand the logic behind testing divisibility only up to the square root, and learn to return appropriate values to signal primality. This lesson helps you boost modularity and efficiency in your C programs by using functions effectively.

We'll cover the following...
C
#include <stdio.h>
int isPrime(int n)
{
if (n <= 1) return 0;
int i;
for (i = 2; (i * i) <= n; i++)
{
if (n % i == 0) return 0;
}
return 1;
}
int main(void)
{
int n = 10;
printf("%d\n", isPrime(n));
return 0;
}
...