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

   

C Program

/* C - Check Prime Number */
#include <stdio.h>
int main(){
    int n; scanf("%d",&n);
    int prime = n > 1;
    for(int i=2;i*i<=n && prime;i++)
        if(n%i==0) prime=0;
    printf(prime ? "Prime" : "Not Prime");
    return 0;
}

C Output

Input:  
5 

Output:  
Prime


C++ Program

// C++ - Check Prime Number
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n; cin>>n;
    bool prime = n > 1;
    for(int i=2;i*i<=n && prime;i++)
        if(n%i==0) prime=false;
    cout<<(prime ? "Prime" : "Not Prime");
}

C++ Output

Input:  
12 

Output:  
Not Prime


JAVA Program

// Java - Check Prime Number
import java.util.*;
class Main{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    boolean prime = n > 1;
    for(int i=2;i*i<=n && prime;i++)
        if(n % i == 0) prime = false;
    System.out.print(prime ? "Prime" : "Not Prime");
  }
}

JAVA Output

Input:  
17 

Output:  
Prime


Python Program

# Python - Check Prime Number
n = int(input())
prime = n > 1
for i in range(2, int(n**0.5) + 1):
    if n % i == 0:
        prime = False
        break
print("Prime" if prime else "Not Prime")

Python Output

Input:  
21 

Output:  
Not Prime


Example
For input 5, the application prints Prime as 5 has no divisors except 1 and itself. But for 12, it prints Not Prime because 12 is divisible by 2, 3, 4, and 6. The algorithm tests all possible divisors up to the square root of the number, which is sufficient to determine primality without unnecessary evaluations.

Real-Life Analogy
Imagine a prime number to be an exclusive VIP club where you can only have two members inside — the number 1 and the number itself. If you attempt to sneak in any other member (divisor), our security system (our loop) detects them and reports "Not Prime." It is similar to security checking at the airport — if there's any extra passenger in the list, you fail the prime check.

Why It Matters
Prime numbers are the fundamental building blocks of mathematics. Like how molecules are composed of atoms, integers may be constructed out of primes by way of multiplication. They're also critical in applied technology, particularly in encryption schemes such as RSA, where prime factorization provides the guarantor of secrecy. Knowing how to verify primality is a central stepping stone into more advanced algorithms like prime sieves and cryptography.

Learning Insights
This code introduces you to algorithmic optimization: rather than testing for divisibility all the way up to n-1, we test only up to sqrt(n), which saves a huge amount of execution time on large numbers. It also shows the value of exiting loops early as soon as one knows the answer, avoiding useless computations. You'll also see the n > 1 check — this is crucial because numbers <= 1 are not prime by definition.

Interview Relevance and Real-World Application
Interviewers are fond of prime number problems as they challenge logical reasoning, math abilities, and code optimization. Examples are to find all primes in an interval, count primes up to N, or produce primes by the Sieve of Eratosthenes. In actual projects, primality tests are applied in hashing functions, cryptography, pseudo-random number generators, and load balancers.

Performance and Edge Observations
Time complexity here is O(√n), which is optimal for single number verifications. Space complexity is O(1). For very large numbers (hundreds of digits), more sophisticated tests such as Miller–Rabin or AKS are employed, particularly in cryptography. The solution given is ideal for competitive programming limits such as numbers up to 10^9.

SEO-friendly summary
This simple and brief prime number code in C, C++, Java, and Python effectively determines whether a provided number is prime by checking for divisibility up to the square root of the number. It's ideal for beginners studying loops, conditional statements, and efficiency optimization in algorithms. The explanation includes why prime numbers are important, how they are used in cryptography, and how interviewers use this problem to assess programming abilities. By mastering this logic, learners can confidently tackle related problems like prime ranges, sieve algorithms, and cryptography-based coding challenges, making this a must-know concept for programming interviews and computer science fundamentals.