In the previous article, we have discussed Java Program to Check Niven Number
In this article we are going to understand what Prime number is and how we can check whether a number is Prime or not in Java with examples.
Java Program to Check Prime Number
Prime numbers are numbers which have no factors except one and itself.
Example: 19-> Prime number 678 -> Not a Prime number 99 -> Not a Prime number
In the above examples the number 19 is only a prime number.
Let’s see different ways to check prime number.
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Approach:
- We ask the user to enter a number which we store in an integer variable
num
. - We then use a while loop to iterate from 2 to the number itself and check if the number is divisible. If divisible then break out of the loop after setting
flag
=false
. - If
flag
istrue
, it is said to be a Prime number or else it is not.
Method-1: Java Program to Check Prime Number By Using Static Value
import java.util.Scanner; public class PrimeNumber { public static void main(String args[]) { //A number is declared int num = 7; //Iter is the iterator which starts from 2 as 1 divided every number int iter = 2; //Flag is used to record if the number is divisible after each iteration boolean flag = true; while (num > iter) { if (num % iter == 0) { flag = false; break; } iter++; } if (flag) { System.out.println(num+" is prime"); } else { System.out.println(num+" is not prime"); } } }
Output: 7 is prime
Method-2: Java Program to Check Prime Number By Using User Input Value
import java.util.Scanner; public class PrimeNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //Iter is the iterator which starts from 2 as 1 divided every number int iter = 2; //Flag is used to record if the number is divisible after each iteration boolean flag = true; while (num > iter) { if (num % iter == 0) { flag = false; break; } iter++; } if (flag) { System.out.println(num+" is prime"); } else { System.out.println(num+" is not prime"); } } }
Output: Enter a number : 97 97 is prime
Method-3: By C Language
#include <stdio.h> int main() { int num, i, flag = 0; printf("Enter a positive integer: "); scanf("%d", &num); for (i = 2; i <= num / 2; ++i) { //if it is divisible by any other number //thenit breaks if (num % i == 0) { flag = 1; break; } } if (num == 1) { printf("1 is unique number"); } else { if (flag == 0) printf("Prime number."); else printf("Not a prime number."); } return 0; }
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Related Java Programs: