How to Check if a Number Is Prime or Not in C++

Опубликовано: 16 Май 2026
на канале: Real Programming Skool (RPS)
5
0

Learn how to check if a number is prime or not in C++ step by step.

In this lesson, we read a number from the user, test it using a loop, and decide whether it is prime or not prime. The program checks if the number can be divided exactly by another number, and if that happens, then the number is not prime.

I also explain the idea in a very simple way for beginners:
A prime number is a number greater than 1 that is divisible only by 1 and itself
Not all odd numbers are prime numbers
The modulo operator % gives the remainder after division
If Number % Counter == 0, that means the number is divisible exactly by Counter
If a number is divisible exactly by another number, then it is not prime

In this video, you will learn:
How to read a number from the user
How to use loop to test divisors
How to use modulo % in C++
How to check if a number is prime or not
Why odd numbers are not always prime
How to return Prime or NotPrime using enum
How to solve a beginner math problem in C++

This lesson is great for C++ beginners who want to improve loops, conditions, enums, modulo, and problem solving.

This's lesson No.26 of Course 3: Problem Solving for Programmers using C++ (part 1) – Beginner Track

► Full programming roadmap (start here):    • How to Start Programming in 2026 (Beginner...  

► Join the RPS community on Telegram:
http://t.me/RPSDiscussionChannel

#cplusplus #cpp #primenumber #primecheck #loops #modulo #beginners #coding #programming #realprogrammingskool