Cheking Prime number --Java string program examples

The number which is only divisible by itself and 1 is known as prime number, for example 7 is a prime number because it is only divisible by itself and 1. This program takes the number (entered by user) and then checks whether the input number is prime or not. The program then displays the result.

Here is an example program

import java.util.Scanner;
class PrimeCheck
{
   public static void main(String args[])
   {		
	int temp;
	boolean isPrime=true;
	Scanner scan= new Scanner(System.in);
	System.out.println("Enter any number:");
	//capture the input in an integer
	int num=scan.nextInt();
        scan.close();
	for(int i=2;i<=num/2;i++)
	{
           temp=num%i;
	   if(temp==0)
	   {
	      isPrime=false;
	      break;
	   }
	}
	//If isPrime is true then the number is prime else not
	if(isPrime)
	   System.out.println(num + " is a Prime Number");
	else
	   System.out.println(num + " is not a Prime Number");
   }
}

Output:

Enter any number:
19
19 is a Prime Number

Source:

Java string examples programs

Recommended Posts

Cheking Prime number --Java string program examples
Java string
java (random number)
Six Prime Number Sequence Advent Calendar 2018 Java Edition
[Java] String padding
Java string processing
I wrote a prime factorization program in Java
Split string (Java)
[Java] String comparison and && and ||
Java string multiple replacement
[Note] Java: String search
About Java String class
Leet string conversion program
[java] Decimal number → n-ary number n-ary number → decimal number
[Java] Calculator program creation 1