Table of Contents
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
Data type and variable in Java
Do-while loop in Java language
Similar post
Java programming code to check prime or not
C programming code to check prime or not