In this video, we’ll learn how to write a simple program in C++ to check whether a given number is prime or not.
The program takes an input number from the user, applies a loop to test divisibility, and then tells if it’s a prime number or not prime.
📌 What you’ll learn in this video:
-
What a prime number is (a number divisible only by 1 and itself).
-
How to use loops and conditions in C++.
-
Why
for (int i = 2; i * i <= num; i++)
is used for optimization. -
Common mistakes to avoid while writing the logic.
🖥️ Program Explanation:
-
User enters a number.
-
If the number is ≤ 1, it’s not prime.
-
Use a loop to check divisibility from 2 up to √n.
-
If divisible, mark as not prime; otherwise, it’s a prime number.