Let’s learn prime number program in java using scanner.

## Prime number program in java using scanner

To check prime number program using scanner first get input from user using nextInt() method of Scanner class.

This user input number is stored in integer variable ‘number’. Then set integer variables ‘s’ and ‘e’ to one and zero.

Repeat and add s = s + 1 while(s <= number). Now if((number % s) == 0) then set e = e + 1.

Lastly if variable e is equal to 2 then given number is prime number else it is not a prime number. Here’s the prime number program using scanner.

import java.util.Scanner; public class PrimeNumberScanner { public static void main(String[] args) { int number, s, e; Scanner sc = new Scanner(System.in); System.out.println("Please enter a number: "); number = sc.nextInt(); s = 1; e = 0; while(s <= number) { if((number % s) == 0) e = e + 1; s++; } if(e == 2) { System.out.println(number + " is a prime number."); } else { System.out.println(number + " is not a prime number."); } sc.close(); } }

**Output:**

Please enter a number: 253

253 is not a prime number.

Please enter a number: 19

19 is a prime number.

Also read – interface in java