All Prime Numbers

 

Given an integer N, print all the prime numbers that lie in the range 2 to N (both inclusive).

Sample Input 1:
9
Sample Output 1:
2
3
5
7
import java.util.Scanner;
public class Solution {

	public static void main(String[] args) {
		
        Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
        
        
        for( int i=2;i<=n;i++)
        {
            int c=0;
            for(int j=1;j<=i;j++)
            {
                if(i%j==0)
                {
                    c++;
                }
            }
            if(c==2)
            {
                System.out.println(i);

            }
            
        }
		
	}
}

Comments

Popular posts from this blog

Minimum Length Word

Check Number Sequence

Star Pattern