Table of Contents
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 ………..
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,
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,
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,
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 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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.