Scanner class Methods in Java

Program to check whether the number is a Prime Number or not?

Prime Number

Prime number is a positive whole number which is only divisible by 1 and itself. We cannot divide a prime number with any other number. Most prime numbers are odd  numbers except 2. Number 2 is the only even prime number.It is divisible by itself and 1 only.

Example: It illustrates Prime numbers till 100.

2,3,5,7,11,13,17,19,23,29.......,97

You can not find 1 in the list of prime numbers because 1 is not a prime number.But why? Lets remove this ambiguity.

There must be distinct factors of a prime number as 2 can be factorized as 2*1. In case of 1, there is no distinct factors.Although 1 is divisible by 1 and itself satisfying the definition of prime numbers , but it does not have any distinct factor and can only be factorized as 1*1.Therefore, 1 is not a prime number.

Now, lets write the program in three different ways.

Method 1:

Write program to find whether the number is prime or not.


Program to find whether the number is prime or not in java

In the above program,
  1. We created a "PrimeNumber" class.
  2. In the main method, we are asking for number as input from user using Scanner object.In the last line of main method, a method "isPrime" is called. This is a static method. To know what are  static methods click here.
  3. After the closing of main, a new method "isPrime(int num)" is defined. This method will take one integer argument or parameter.When we call this method from main , we have to pass the number inputted by user to it.
  4. The boolean variable "flag" is  used to hold the true and false value. The "flag"  is set to true for number being prime and false otherwise. 2 is a prime number and when user enters 2, flag will hold true value.
  5. The variable "testNumber"initialized with the square root of number.This variable is used to check condition in for loop. The for loop will work till i is less than or equal to "testNumber". 
  6. In loop,  if statement is used to check whether the number is divisible by the value of "i". In here, num%i ==0 will check if remainder on dividing number with value of "i" is 0 . If remainder is 0 then number is not prime. So, flag is set to false when number is divisible by "i".
  7. The else statement sets the value of flag to true because when remainder is not 0 it means number is only divisible by itself and 1.
  8. The last if-else statements are used to print the result on screen. When flag is true,the if block will print number is prime and else block will print number is not prime.
Output:
 
prime number


Method 2:

Write program to find whether the number is prime or not.


Write program to find whether the number is prime or not.

In the above program,
  1. In the last line of main method, a method "isPrimeOrNot" is invoked.
  2. In the static method "isPrimeOrNot(int  num)" , several if statements are used.Lets understand each one by one.
  3. If the number is less than 0. First if statement will return error message that this is an invalid number
  4. If  entered number is 0 or 1, it is not prime.
  5. If entered number is 2 or 3, it is prime.
  6. Last if statements checks for number that is greater than 3. Put num=5 in this if statement and lets understand. if ( (5* 4)%24==0), will be 20%24 which is not equal to 0. So, 5 is a prime number.
  7. The else block will execute when the remainder is 0.

Output:

prime or not


Method 3:

Program to find whether the number is prime or not.



If you understood Method 1 and 2 then this not difficult to understand.

The method "isPrimee(int num)" is a boolean method, it will return true and false only. When the number is prime , true is returned on screen. When not prime false is returned.

Output:

prime number



Happy Coding!

Comments