A natural number that is greater than 1 , divided by 1 and itself is called prime number. In this tutorial, we are going to write a program to print prime numbers up to given number.

import java.util.Scanner;
public class PrimeNumberUptoN {
 
    public boolean isPrimeNumber(int number){
	    if (number == 0 || number==1)
		{
			return false ;
		}
		if (number == 2)
		{
			return true ;
		}
        for(int i=2; i<=number/i; i++){
            if(number % i == 0){
                return false;
            }
        }
        return true;
    }
    public static void main(String args[]){
           PrimeNumberUptoN primenumber = new PrimeNumberUptoN();
		   System.out.print("Please Enter Number: ");
           Scanner read = new Scanner(System.in);
           int number = read.nextInt();
		   for (int i=2;i<=number ;i++ )
		   {
		   if(primenumber.isPrimeNumber(i))
			   {
			   System.out.print(i +"   ");
			   }
		   }
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *