## C++ Program:

- To print prime numbers between 1 & N

#### Example:

User Input: 12

Output: 2,3,5,7,11

``````#include <iostream>
using namespace std;

int prime(int n){
if(n<2)return 0;
for(int i=2; i<=n/2; i++) {
if(n%i==0) return 0;
}
return 1;
}

int main() {
int num;
cout<<"Enter any +ve number : ";
cin>>num;
cout<<"Your prime numbers between 1 & "<<n<<" are : \n";
for(int i=2; i<num; i++)
if(prime(i))cout<<i<<"  ";
return 0;
}``````

### Output:

```  Enter any +ve number : 20
Your prime numbers between 1 & 20 are :
2 3 5 7 11 13 17 19```
Program
number

You should know first :

Prime numbers: Numbers greater than 1 & have only 2 factors ( 1 and itself )

Main Logic to check prime :

``````int prime(int n){
for(int i=2;i<=n/2;i++) {
if(n%i==0) return 0;
}
return 1;
}``````

We are running the loop `n/2` Since, any number can have only factors under its half, except itself

To print all primes :

``````for(int i=2; i<num; i++) {
//check every number for prime [ if(prime(i)) ], if prime then print the number
}
``````
Concept

Coming Soon !

Quick
Tutorial

# Quote

Be happy for this moment. This moment is your life.