Java Program to check Prime Number

In this tutorial you will learn about the Java Program to check Prime Number and its application with practical example.

In this tutorial, we will learn to create a Java Program to check Prime Number using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.
  • User Define functions

What is Prime Number?

A  number is called a Prime number if a number is divisible by 1 or itself only.

For example 2, 3, 5, 7, 11, 13, 17..are some prime numbers.

Java Program to check Prime Number

In this program we would find the given number is a Prime Number or not .First of all we take a value form user and find the number is prime or not.Let have a look at the code.

Output

Prime number

Not a Prime number.

In the above program, we have first declared and initialized a set variables required in the program.

  • number= it will hold entered number.
  • temp = it will hold temporary number.
  • flag=  it will hold the result.
  • i = for iteration

After declaring variables we take a value from user and find number is prime or not.

with in loop we will find that given number is prime or not where we divide number from 2 to number if number divide by it self only value of flag variable remain true

else set to false it means number is not a prime number

so in this blog we have find the given number is a prime number or not.

In this tutorial we have learn about the Java Program to check Prime Number and its application with practical example. I hope you will like this tutorial.