Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
C++ code to check whether the number is prime or not

Java programming code to check prime or not

Posted on October 14, 2020October 15, 2020

Table of Contents

  • Java programming code to check prime or not
    • Programming code to check prime or not
      • Program to check whether a number is prime or not -using for loop
      • Program to check whether a number is prime or not -using while loop
      • Program to check whether a number is prime or not -using do-while loop
      • Program to check whether a number is prime or not -using method
      • Program to check whether a number is prime or not -using recursion

Java programming code to check prime or not

In this tutorial, we will discuss the Java programming code to check prime or not

In this post, we are going to learn how to check whether the given number is prime or not  using 5 ways in Java language

This is done using for loop, while loop, do-while loop method and recursion using Java programming code to check prime or not

 

A prime number is a number which is greater than(positive) one and divisible by only two numbers: 1 and it self. when any number is divisible by any other number it is not a prime number.

So, prime numbers can,t be divided by other numbers than itself or 1 Eg 2,3,5,7,11,13,17 ………..

Programming code to check prime or not

Program to check whether a number is prime or not -using for loop

Program 1

import java.util.Scanner;
class CheckPrimeornot{
public static void main(String args[]){
int num, i,count=0;
    Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  System.out.print("\nEnter the number: ");

num=scan.nextInt();//get input from the user for num
for(i=2; i<num; i++){
if(num%i==0){
count++;
break;
}
}
if(count==0){
System.out.print("\nthis is a prime number ");
}
else{
System.out.print("\nthis is not a prime number ");
}
}
}

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

Case 1

Enter the number: 19
this is a prime number

Case 2

Enter the number: 28
this is not a prime number

 

In this program,

  • integer variable num,i  are declared.
  • integer variable count=0  is declared and initialized
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using Scanner class and store to the variable num
  • create a for loop of i from 1 to n and increase the value of i after every iteration by 1
  • Calculate mod of every value of i and check whether the result is equal to zero or not
  • when the if statement returns true then the count value is increased by 1 until the test condition is false
  • The given numbers are tested whether it is prime or not using the count value
  • if the value of the count is equal to 0 then the number is prime else  the number is odd
  • Then,the program is displayed the output using System.out.print() function.

Program to check whether a number is prime or not -using while loop

Program 2

import java.util.Scanner;
class CheckPrimeornotwhile{
public static void main(String args[]){
int num, i,count=0;
    Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  System.out.print("\nEnter the integer number: ");

num=scan.nextInt();//get input from the user for num
i=2; 
while(i<num){
if(num%i==0){
count++;
break;
}
 i++;
}
if(count==0){
System.out.print("\nthis is a prime number ");
}
else{
System.out.print("\nthis is not a prime number ");
}
}
}

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

 

Case 1

Enter the number: 31
this is a prime number

Case 2

Enter the number: 51
this is not a prime number

 

In this program,

  • integer variable num,i  are declared.
  • integer variable count=0  is declared and initialized
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using Scanner class and store to the variable num
  • Create a while loop of i from 1 to n and increase the value of i after every iteration by 1
  • Calculate mod of every value of i and check whether the result is equal to zero or not
  • when the if statement returns true then the count value is increased by 1 until the test condition is false
  • The given numbers are tested whether it is prime or not using the count value
  • If the value of the count is equal to 0 then the number is prime else  the number is odd
  • Then,the program is displayed the output using System.out.print() function.

 

Program to check whether a number is prime or not -using do-while loop

Program 3

import java.util.Scanner;
class CheckPrimeornotdowhile{
public static void main(String args[]){
int num, i,count=0;
    Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  System.out.print("\nEnter the integer number: ");

num=scan.nextInt();//get input from the user for num
i=2; 
do{
if(num%i==0){
count++;
break;
}
 i++;
}while(i<num);
if(count==0){
System.out.print("\nthis is a prime number ");
}
else{
System.out.print("\nthis is not a prime number ");
}
}
}

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

Case 1

Enter the number: 37
this is a prime number

Case 2

Enter the number: 49
this is not a prime number

 

In this program,

  • integer variable num,i  are declared.
  • integer variablec ount=0  is declared and initialized
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using Scanner class and store to the variable num
  • create a do-while loop of i from 1 to n and increase the value of i after every iteration by 1
  • Calculate mod of every value of i and check whether the result is equal to zero or not
  • when the if statement returns true then the count value is increased by 1 until the test condition is false
  • The given numbers are tested whether it is prime or not using the count value
  • if the value of the count is equal to 0 then the number is prime else  the number is odd
  • Then,the program is displayed the output using System.out.print() function.

 

Program to check whether a number is prime or not -using method

Program 4

import java.util.Scanner;
class CheckPrimeornotmethod{
public static void main(String args[]){
int num;
    Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  System.out.print("\nEnter the positive number: ");

num=scan.nextInt();//get input from the user for num

boolean is_primenum=checkPrime(num);//method call
if(is_primenum){//check prime number
  System.out.print(num+" is a prime number");
}
else{
  System.out.print(num+" is not a prime number ");
}
}


static boolean checkPrime(int num){//method definition
  boolean isPrime=true;
  if(num<=1){
    isPrime=false;
    return isPrime;
  }
  else{
    for(int i=2; i<=num/2; i++){
if(num%i==0){
isPrime=false;
break;
}
}
return isPrime;
  }
}
}

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

 

case 1

Enter the positive number: 19
19 is a prime number

case 2

Enter the positive number: 25
25 is not a  prime number

Program to check whether a number is prime or not -using recursion

Program 5

import java.util.Scanner;
class CheckPrimeornotrec{
public static void main(String args[]){
int num;
    Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  System.out.print("\nEnter the integer number: ");

num=scan.nextInt();//get input from the user for num


if(isPrime(num)){//check prime number
  System.out.print(num+" is a prime number");
}
else{
  System.out.print(num+" is not a prime number ");
}
}


static boolean isPrime(int num){//method definition
  boolean isPrime=true;
  if(num<=1){
    return false;
    
  }
  else{
    for(int i=2; i<num; i++){
if(num%i==0){
return false;
}
}
return true;
  }
}
}

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

 

case 1

Enter the positive number: 23
23 is a prime number

case 2

Enter the positive number: 33
33 is not a  prime number

 

Suggested for you

if statements in Java language

Nested if in Java language

Data type and variable in Java

Operator in Java language

Method in Java language

For loop in Java language

while loop in Java language

Do-while loop in Java language

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes