Write a C program to find whether a given number is prime or not

What is a Prime Number?

A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. In simpler terms, a prime number is only divisible by 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers, while 4, 6, 8, and 9 are not, as they can be divided evenly by numbers other than 1 and themselves.

C Program

#include <stdio.h>

// Function to check if a number is prime
int isPrime(int num) {
    if (num <= 1) {
        return 0; // 0 and 1 are not prime numbers
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            return 0; // If the number is divisible by any i, it's not prime
    return 1; // If no divisors found, the number is prime

int main() {
    int number;
    // Input the number
    printf("Enter a number: ");
    scanf("%d", &number);
    // Check if the number is prime
    if (isPrime(number)) {
        printf("%d is a prime number.\n", number);
    } else {
        printf("%d is not a prime number.\n", number);
    return 0;

This program defines a function isPrime that takes an integer as an argument and returns 1 if the number is prime and 0 otherwise. The main function takes user input, calls the isPrime function, and prints the result.

Read my other blogs:

Write a program to find Factorial Numbers of a given numbers.

Embedded C language Interview Questions.

Automotive Interview Questions

Understanding AUTOSAR Architecture: A Guide to Automotive Software Integration



Types of ECU in CAR

Big Endian and Little Endian in Memory

Zero to Hero in C language Playlist

Embedded C Interview Questions

Subscribe my channel on Youtube: Yogin Savani

Leave a Comment

Your email address will not be published. Required fields are marked *