# How to Make Series of Prime Numbers in C++

Starting and understanding C++ is not much difficult but it needs good concentration to understand the logic of c++. One have to practice more and more to find the way and the logic of thinking in c++. After studying some basic concepts and topics in c++ such as: Finding IDE for working on, Understanding the Structure of the C++, Libraries, Data-Types, Variables, Loops and etc… you can write and run basic programs. One of the programs which is often used to understand the mentioned concept better is to;

**Read : [button link=”http://codejow.com/array-data-structure-explanation-c/” target=”blank” color=”blue” icon=”none” style=”normal” size=”medium”]Arrays in c++[/button] [button link=”http://codejow.com/group-related-values-different-data-types-c/” target=”blank” color=”blue” icon=”none” style=”gloss” size=”medium”]Struct in C++[/button]**

## Write a program that should print or make series of the Prime Numbers** :**

First we have to that actually what is a **prime number **and its series?

Ans: A number that is divisible only by itself and 1.

There are different ways to write a program that prints the prime number series. I am going to describe and explain a simple way to write the mentioned program. You have to about Loops, Variables and Basic Concept of C++.

- Create new c++ file inside your IDE, include the library
*<iostream>,*place*std namespace*and*main function.* - Write codes to ask
**Prime Number**series. - Put a loop like
*for*to repeat the process of finding the**Prime****Number**untill the it reaches the length entered by the user.

[textmarker color=”1FC240″ type=”background color”]

Algorithm to check whether a number is prime number or not

Let, N be a positive number.

For every number i, between 2 to N/2(2<= i <= N/2) check whether i divides N completely(check If i is a factor of N). if (N % i == 0), then N cannot be a Prime number.

If none of the number between 2 to N/2 divides N completely then N is a prime number.

[/textmarker]

Check out the program below:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include<iostream> #include<conio.h> using namespace std; int main(){ int N, i, j, isPrime, n; cout<<"To print all prime numbers between 1 to N\n"; cout<<"Enter the value of N: "; cin>>N; /* For every number between 2 to N, check whether it is prime number or not */ cout<<"Prime numbers between 1 to "<<N<<" is: "; for(i = 2; i <= N; i++){ isPrime = 0; /* Check whether i is prime or not */ for(j = 2; j <= i/2; j++){ /* Check If any number between 2 to i/2 divides I completely If yes the i cannot be prime number */ if(i % j == 0){ isPrime = 1; break; } } if(isPrime==0 && N!= 1) cout<<i<<" "; } getch(); return 0; } |

The OutPut:

Feel free to share your comments and ask questions!