Prime Numbers in C Programming

Prime Numbers in C Programming – Complete Guide

What is a Prime Number?

A prime number is a natural number greater than 1 that has only two factors: 1 and itself.

  • Examples → 2, 3, 5, 7, 11, 13 …

  • Not Prime (Composite) → 4 (factors: 1,2,4), 6 (factors: 1,2,3,6)

Logic to Check Prime Number in C

  • Take a number n as input.

  • If n <= 1, it’s not prime.

  • Check divisibility from 2 to n/2 (or √n for efficiency).

  • If divisible, then it’s not prime.

  • Otherwise, it’s prime.

#include <stdio.h>

int main() {
    int n, i, flag = 0;
    printf("Enter a number: ");
    scanf("%d", &n);

    if (n <= 1) {
        printf("%d is not a prime number.\n", n);
        return 0;
    }

    for (i = 2; i <= n/2; i++) {
        if (n % i == 0) {
            flag = 1;  // Not prime
            break;
        }
    }

    if (flag == 0)
        printf("%d is a prime number.\n", n);
    else
        printf("%d is not a prime number.\n", n);

    return 0;
}

Example Run

 
Enter a number: 7
7 is a prime number.

Optimized Code (Check till √n only)

#include <stdio.h>
#include <math.h>

int main() {
    int n, i, flag = 0;
    printf("Enter a number: ");
    scanf("%d", &n);

    if (n <= 1) {
        printf("%d is not a prime number.\n", n);
        return 0;
    }

    for (i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
            flag = 1;
            break;
        }
    }

    if (flag == 0)
        printf("%d is a prime number.\n", n);
    else
        printf("%d is not a prime number.\n", n);

    return 0;
}

Checking up to √n reduces execution time for large numbers.

Program to Print Prime Numbers in a Range

#include <stdio.h>

int main() {
    int start, end, i, j, flag;

    printf("Enter start and end values: ");
    scanf("%d %d", &start, &end);

    printf("Prime numbers between %d and %d are: ", start, end);

    for (i = start; i <= end; i++) {
        if (i <= 1)
            continue;

        flag = 0;
        for (j = 2; j <= i/2; j++) {
            if (i % j == 0) {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
            printf("%d ", i);
    }
    return 0;
}

Example Run

 
Enter start and end values: 10 30
Prime numbers between 10 and 30 are: 11 13 17 19 23 29

Key Points

  • First prime number is 2 (smallest and only even prime).

  • Prime checking is important in cryptography, hashing, and algorithms.

  • Optimization is important when working with large numbers.

  • Related Posts

    About C Programming Basic

    C Programming Basics – A Beginner’s Guide Introduction C is one of the oldest and most powerful programming languages, created by Dennis Ritchie in 1972.It is often called the “mother…

    What is arrays in c programming

    Array in C Programming – Explained for Beginners Introduction In C programming, an array is a collection of elements of the same data type stored at contiguous memory locations. It…

    Read More

    How to Create Database in MySQL

    • By admin
    • November 27, 2021
    • 34 views
    How to Create Database in MySQL

    How to create table in MySQL

    • By admin
    • November 6, 2021
    • 30 views
    How to create table in MySQL

    MySQL commands with examples

    • By admin
    • September 11, 2021
    • 76 views
    MySQL commands with examples

    MySQL use database

    • By admin
    • May 28, 2021
    • 30 views
    MySQL use database

    System Software

    • By admin
    • May 20, 2021
    • 40 views

    Introduction to software

    • By admin
    • May 13, 2021
    • 33 views
    Introduction to software