👋 Introduction
If you're just starting out in programming, one of the classic exercises you’ll encounter is checking whether a number is a prime number.
But wait—what is a prime number again?

In this guide, we’ll:
Let’s dive in!
A prime number is a number that is:
2, 3, 5, 7, 11, 13, 17...
❌ Not prime:
To check if a number is prime:
1. Input a number n
2. If n <= 1, it's not prime
3. Loop from 2 to n - 1:
4. If no number divides n, it's prime!
You can also optimize the loop to run from 2 to √n (square root of n), but we’ll get to that later.
c
CopyEdit
Copy Code
#include <stdio.h>
int main() {
int n, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n <= 1) {
isPrime = 0;
} else {
for (i = 2; i < n; i++) {
if (n % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);
return 0;
}python
CopyEdit
Copy Code
num = int(input("Enter a number: "))
if num <= 1:
print(num, "is not a prime number")
else:
for i in range(2, num):
if num % i == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")java
CopyEdit
Copy Code
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
boolean isPrime = true;
if (n <= 1) {
isPrime = false;
} else {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
System.out.println(n + " is a prime number.");
else
System.out.println(n + " is not a prime number.");
}
}You don’t need to check all numbers till n. Just check till the square root of n. It speeds things up, especially for large numbers.
Python Version:
python
CopyEdit
Copy Code
import math
num = int(input("Enter a number: "))
isPrime = True
if num <= 1:
isPrime = False
else:
for i in range(2, int(math.sqrt(num)) + 1):
if num % i == 0:
isPrime = False
break
if isPrime:
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")| Mistake | Why It Happens | Fix |
| Checking prime for 1 | 1 is not a prime | Start from 2 |
| Forgetting to break loop | Wastes time after finding a factor | Use break |
| Wrong loop condition | Going up to n + 1 instead of n | Loop till n-1 or √n |
less
CopyEdit
Enter a number: 7
7 is a prime number.
Enter a number: 8
8 is not a prime number.
| Application Area | Purpose |
| Cryptography | Secure key generation |
| Coding Interviews | Common test problem |
| Competitive Coding | Primality tests, sieve algorithm |
| Math Libraries | Efficient algorithms like Miller-Rabin |
Want to go from writing basic programs to solving real-world coding challenges?
Check out Uncodemy’s Programming Courses—suitable for beginners, students, and professionals.
✅ Learn C, C++, Python, Java, JavaScript
✅ Strong foundation in logic building
✅ Data Structures + Algorithms
✅ Real coding assignments & projects
✅ Placement assistance and certification
Start with a prime number. Build your way to prime skills!
| Concept | Description |
| Prime Number | Only divisible by 1 and itself |
| Key Condition | n % i == 0 → Not prime |
| Edge Case | n <= 1 is not prime |
| Optimization | Loop till √n |
| Tools | C, Python, Java, etc. |
A prime number program might be a beginner exercise, but it teaches you looping, logic, and optimization—skills every good programmer needs.
Start simple. Practice regularly. And if you want to level up fast, explore Uncodemy’s courses and start building real programming confidence.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR