Sure! Here’s an example of a program in C that checks whether a given number is prime or not:
#include <stdio.h> int isPrime(int num) { if (num <= 1) { return 0; // not a prime number } for (int i = 2; i * i <= num; i++) { if (num % i == 0) { return 0; // not a prime number } } return 1; // prime number } int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (isPrime(num)) { printf("%d is a prime number.\n", num); } else { printf("%d is not a prime number.\n", num); } return 0; }
In this program, the isPrime
function takes an integer as input and checks whether it is prime or not. It follows the basic primality test algorithm, where it iterates from 2 to the square root of the number and checks if any number divides the given number. If a divisor is found, the number is not prime.
In the main
function, the user is prompted to enter a number, which is then passed to the isPrime
function. Based on the return value, the program displays whether the number is prime or not.
Note: This program assumes that the input number is an integer greater than or equal to zero.