EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Prime Number in C++

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » Prime Number in C++

Prime Number in C++

Introduction to Prime Number in C++

What is the prime number? Any number which is greater than 1 and it should either be divided by 1 or the number itself is called a prime number. As prime numbers cannot be divided by any other number it should only be the same number or 1. For example here is the list of Prime Number in C++ that are divisible by either 1 or number itself.

List of some Prime Numbers

2 3 5 7 11 13 17 19 23 29 31 37 41…

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

You might be thinking why 2 is considered as a prime number? Well, it’s an exception therefore 2 is the only prime number in the list which is also even. Only two numbers are consecutive natural numbers which are prime too! Also, 2 is the smallest prime number.

The logic behind the prime number is that if you want to find prime numbers from a list of numbers then you have to apply the mentioned below logics:

If the given number is divisible by itself or 1, 2 is the only even prime number which is an exception so always remember. Divide the given number by 2, if you get a whole number then number can’t be prime!

Except 2 and 3 all prime numbers can be expressed in 6n+1 or 6n-1 form, n is a natural number.

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,837 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

There is not a single prime number that ends with 5 which is greater than 5. Because logically any number which is greater than 5 can be easily divided by 5.

For a more clear explanation that supports all the above-given logic here is the table of all the prime numbers up to 401:

2 3 5 7 11 13 17 19 23
29 31 37 41 43 47 53 59 61 67
71 73 79 83 89 97 101 103 107 109
113 127 131 137 139 149 151 157 163 167
173 179 181 191 193 197 199 211 223 227
229 233 239 241 251 257 263 269 271 277
281 283 293 307 311 313 317 331 337 347
349 353 359 367 373 379 383 389 397 401

Prime Numbers Using Various Methods

Now let’ see how to find prime numbers using various methods such as for loop, while loop, do-while loop. The output will be the same in all three loop cases because logic is the same only implementing way is different.

We will see that through a C ++ code separately for every loop.

Example #1

Finding a prime number using for loop

Code:

#include <iostream>
#include <math.h>
using namespace std;
int main() {
int x; //  Declaring a variable x
cout << "Please enter the number : "; //  cout to get the input value from user
cin >> x;
cout << "Here is the list of all the prime numbers Below "<< x << endl;
for ( int m=2; m<x; m++)      //implementing for loop to find out prime numbers
for ( int n=2; n*n<=m; n++)
{
if ( m % n == 0)
break;
else if ( n+1 > sqrt (m)) {
cout << m << endl;
}
}
return 0;
}

Output:

Prime Number in C++

As you can see in the above code we have taken two for loops because we need a list of prime numbers that will be below the given number in our program. We have included for loop within another for loop to make our calculation easier. A condition is added through if statement to break the loop once we reach our given number in code.

Example #2

Finding a prime number using for loop with if-else

Code:

#include <iostream>
using namespace std;
int main ()
{
int number, x, count = 0;
cout << "Please enter the number to check if it's prime or not : " << endl;
cin >> number;
if ( number == 0)
{
cout << "\n" << number << " This number is not prime";
exit(1);
}
else   {
for ( x=2; x < number; x++)
if ( number % x == 0)
count++;
}
if ( count > 1)
cout << "\n" << number << " This number is not prime.";
else
cout << "\n" << number << " This is prime number.";
return 0;
}

Output:

prime number in C++

Example #3

Finding a prime number using WHILE loop with if-else

Code:

#include <iostream>
using namespace std;
int main()
{
int lower, higher, flag, temporary;
cout << "Please enter the two numbers for finding prime numbers between them: "<< endl;
cin >> lower >> higher;
if ( lower > higher) {    //It will swap the numbers if lower number is greater than higher number.
temporary = lower;
lower = higher;
higher = temporary;
}
cout << "Hence the Prime numbers between the number " << lower << " and " << higher << " are: "<< endl;
while ( lower < higher)
{
flag = 0;
for ( int x = 2; x <= lower/2; ++x)
{
if ( lower % x == 0)
{
flag = 1;
break;
}
}
if ( flag == 0)
cout << lower << " ";
++lower;
}
return 0;
}

Output:

prime number in C++

In the above code, we have taken integers as a lower number, higher number, temporary variable, and a flag. Initially, we take two numbers as an input one is lower while the other is higher. In case the lower number is bigger than the higher number then these numbers will be swapped through a temporary variable first to move further in code. Now while loop will follow up until lower is less than higher and for loop, the condition will keep calculating prime numbers between them.

Conclusion

In, prime number logic can be used not only in C++ but in any programming language. From a small set of numbers to a big amount of numbers this logic can be used to find a set of prime numbers according to requirements within seconds without wasting any time in computer programming.

Recommended Articles

This is a guide to Prime Number in C++. Here we discuss the list of some prime numbers, and Various methods used in for Prime Numbers. You can also go through our other suggested articles to learn more–

  1. ifstream in C++
  2. Swapping in C++
  3. Abstraction in C++
  4. Regular Expressions in C++

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More