Check Prime Number in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

 

C Program

#include<stdio.h>

int main() {
    int n, f = 1;
    scanf("%d", &n);
    for (int i = 2; i < n; i++)
        if (n % i == 0) f = 0;
    printf(n > 1 && f ? "Prime" : "Not Prime");
}

C Output

Input: 7  
Output: Prime


C++ Program

#include<iostream>
using namespace std;

int main() {
    int n, f = 1;
    cin >> n;
    for (int i = 2; i < n; i++)
        if (n % i == 0) f = 0;
    cout << (n > 1 && f ? "Prime" : "Not Prime");
}

C++ Output

Input: 9  
Output: Not Prime


JAVA Program

import java.util.*;

class P {
    public static void main(String[] a) {
        int n = new Scanner(System.in).nextInt(), f = 1;
        for (int i = 2; i < n; i++)
            if (n % i == 0) f = 0;
        System.out.print(n > 1 && f == 1 ? "Prime" : "Not Prime");
    }
}

JAVA Output

Input: 13  
Output: Prime


Python Program

n = int(input())
print("Prime" if n > 1 and all(n % i for i in range(2, n)) else "Not Prime")

Python Output

Input: 10  
Output: Not Prime


In-Depth Learning – Entire Concept in Paragraphs
What Is a Prime Number?
A prime number is a number that is larger than 1 and has only one positive divisor besides 1 and itself. That is, if a number can be divided evenly by any other number besides 1 and itself, then it is not prime. For instance, 2, 3, 5, 7, 11, and 13 are prime numbers, whereas 4, 6, 8, 9, and 12 are not since they have divisors other than 1 and themselves. The building blocks of mathematics, especially of number theory and cryptography, are prime numbers.

How the Prime Check Works
In order to determine if a number is prime, we can divide it by all the numbers from 2 up to n-1. If it's divisible by any one of them, then it's not prime. If no number like that divides it, then it's a prime number. We don't include 1 because by definition, 1 isn't a prime. This is a straightforward method and is effective for relatively small numbers. For big values, such optimizations as until sqrt(n) or even numbers could be skipped are possible, but for educational purposes and seeing the logic, until n-1 is ideal.

Example
Let's test if 7 is a prime number. We test for divisibility starting from 2 to 6:

7 % 2 ≠ 0

7 % 3 ≠ 0

7 % 4 ≠ 0

7 % 5 ≠ 0

7 % 6 ≠ 0
Because 7 has no other divisor but 1 and 7, it is prime. But if we try 9, we get 9 % 3 == 0, indicating that it has another divisor and is not prime.

Real-Life Analogy
Imagine a prime number as an individual with a very personal life — they only invite two individuals: themselves and their dearest relationship (the number 1). Composite numbers, conversely, are like open-door socializers — they possess several connections (divisors). Primes, in security systems such as current encryption, are employed because their uniqueness (no easy divisors) renders them extremely difficult to break down. The fact that large primes are difficult to factor is what secures your bank transactions!

How the Code Works
The code sets a flag f = 1 (presuming the number to be prime). It then goes through each number from 2 through n-1. When it encounters any number that can evenly divide n (n % i == 0), it makes the flag 0. Then, upon termination of the loop, the application checks whether n is more than 1 and the flag remains 1. If both conditions hold true, then the number is prime. Otherwise, not. The Python implementation does this even more gracefully using all(n % i for i in range(2, n)), which is True if n is not divisible by any i (i.e., it's prime).

Why Prime Check Is Important
Prime numbers are essential across a range of computer science and mathematical domains. They are central to RSA encryption, hashing, modular arithmetic, and even blockchain technology. Working with checking prime numbers educates you about loops, conditionals, logical operators, and problem-solving strategies. Prime checking is a popular interview and classroom exercise because it's easy to describe but leads into higher-level optimizations and mathematics-based thinking.

SEO-Optimized Natural Paragraph for Ranking
If you're learning to verify if a number is prime, this tutorial provides you with the most elegant and beginner-friendly solution in C, C++, Java, and Python. The examples are minimalistic yet complete, tailored to educate logic and readability. With descriptions extending beyond syntax alone — through analogies of real-world applications, mathematical formulations, and breakdowns of examples — this page not only assists students in writing functional code but also explaining how prime checking functions. This topic finds wide application in coding interviews, competitive coding, and cryptographic applications. As a student, job seeker, or hobbyist, becoming proficient in prime number reasoning is a fundamental stepping stone toward higher-level programming and algorithmic reasoning.