Check value

Example program to check whether a Number is Prime or Not in Java

Example program to check whether a Number is Prime or Not in Java

In this tutorial, we will discuss the concept of an Example  program to check whether a Number is Prime or Not in Java

In this post, we are going to learn how to check a number is prime or not using for, while, and do-while loop in Java programming language:

 

The number that can be divided by 1 and itself. it is called prime number

for Example 2,3,5,7,11,13….

Check prime or not

To understand this example program, you should have previous knowledge of the following Java topics

for loop in Java language

while  loop in Java language

do-while  loop in Java language

if statement in Java language

 

Example program to check whether a Number is Prime or Not

Find the prime number using the for loop

In this program, we will check whether the given number is prime or not, using for loop, first, the user is asked to enter a number randomly, then the program checks if the given number is prime or not

Program 1

import java.util.Scanner;
class Check_Prime{
public static void main(String args[]){
int num;
boolean isPrime=false;
    Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
System.out.print("Enter a number: ");
num=scan.nextInt();//get input from the user for num1

for(int i=2; i<=num/2; i++){
    
    //condition for non-prime
    if(num%i==0)
    {
        isPrime=true;
        break;
    }
}

if(!isPrime){
    System.out.println(num+" is a prime numbner ");
}
else{
    System.out.println(num+" is not a prime numbner ");
}
}
}

When the above code is executed, it produces the following results

Case 1

Enter a number: 23
23 is a prime number

 

Case 2

Enter a number: 54
54 is not a prime number

 

This program allows the user to enter a positive number and then it will check the given number is a prime number or not using for loop in Java language

 

Find the prime number using the while loop

In this program, we will check whether the given number is prime or not, using while loop, first, the user is asked to enter a number randomly, then the program checks if the given number is prime or not

Program 2

import java.util.Scanner;
class Check_Prime1{
public static void main(String args[]){
int num;
boolean isPrime=false;
    Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
System.out.print("Enter a number: ");
num=scan.nextInt();//get input from the user for num1

int i=2;
while( i<=num/2){
    
    //condition for non-prime
    if(num%i==0)
    {
        isPrime=true;
        break;
    }
     i++;
}

if(!isPrime){
    System.out.println(num+" is a prime numbner ");
}
else{
    System.out.println(num+" is not a prime numbner ");
}
}
}

When the above code is executed, it produces the following results

Case 1

Enter a number: 12
12 is not a prime number

 

Case 2

Enter a number: 37
37 is  a prime number

 

This program allows the user to enter a positive number and then it will check whether the given number is a prime number or not, using a while loop in the Java language

 

 

 

Find the prime number using the do-while loop

In this program, we will check whether the given number is prime or not, using do-while loop, first, the user is asked to enter a number randomly, then the program checks if the given number is prime or not

Program 3

import java.util.Scanner;
class Check_Prime2{
public static void main(String args[]){
int num;
boolean isPrime=false;
    Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
System.out.print("Enter a number: ");
num=scan.nextInt();//get input from the user for num1

int i=2;
do{
    
    //condition for non-prime
    if(num%i==0)
    {
        isPrime=true;
        break;
    }
     i++;
}while( i<=num/2);

if(!isPrime){
    System.out.println(num+" is a prime numbner ");
}
else{
    System.out.println(num+" is not a prime numbner ");
}
}
}

When the above code is executed, it produces the following results

Case 1

Enter a number: 100
100 is  not a prime number


Case 2

Enter a number: 101
101 is not a prime number

 

This program allows the user to enter a positive number and then it will check whether the given number is a prime number or not using the do-while loop in the Java language

 

Suggested for you

Data type and variable in Java language

The operator in Java language

 

Similar post

Program to check whether a Number is Prime or Not in C++

Program to check whether a Number is Prime or Not in C

Program to check whether a Number is Prime or Not in Java

Program to check whether a Number is Prime or Not in Python

 

 

Example program to check whether a Number is Prime or Not in C
C++ example to check whether a Number is Prime or Not
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago