## Java Program:

- To print prime numbers between 1 & N

#### Example:

User Input: 12

Output: 2,3,5,7,11

``````import java.util.Scanner;

public static boolean prime(int n) {
if(n<2)return false;
for(int i=2; i<=n/2; i++) {
if(n%i==0) return false;
}
return true;
}

public static void main(String []args) {
int num;
Scanner userInput = new Scanner(System.in);
System.out.print("Enter any +ve number : ");
num = userInput.nextInt();

System.out.println("Your prime numbers between 1 & "+num+" are :");
for(int i=2; i<num; i++)
if(prime(i))System.out.print(i+" ");
}
}``````

### 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 :

``````public static boolean prime(int n){
if(n<2)return false;
for(int i=2;i<=n/2;i++) {
if(n%i==0) return false;
}
return true;
}``````

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.