What is a prime number?
A prime number is a natural number greater than 1 that has only itself and the number 1 as its only other positive divisors.
In this blog, we’ll understand what prime numbers are as well as how to create a Java program to find them. Let’s dive into the code now.
Prime Number Program in Java:Code and Output
Let’s now look at how to create a Java program that finds prime numbers. An integer is entered into the program, and it determines whether or not it is a prime number
import java.util.*; // wildcard Scanner import public class PrimeNumber { public static void main(String[] args) { int num; boolean isPrime = true; Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); num = input.nextInt(); input.close(); for(int a = 2; a <= num/2; a++) { if(num % a == 0) { isPrime = false; break; } } if(isPrime) System.out.println(num + " is a prime number."); else System.out.println(num + " is not a prime number."); } }
Output:
If the user enters 17 as input, the program will output:
Enter a number: 17
17 is a prime number.
If the user enters 15 as input, the program will output:
Enter a number: 15
15 is not a prime number.
This confirms that the program correctly identifies if a number is prime or not.
Know the Fibonacci series in Java here!
Definition of prime number:
Any natural number greater than 1 with no positive divisors besides itself and 1 is known as a “prime number.” To give just a few examples, prime numbers include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 73, 79, 83, 89, and 97.
How to Write a Java Program for Prime Number
Writing a Java program to determine whether a given number is prime or not is a typical programming exercise that can help you understand basic concepts like loops, conditional statements, and methods are as follows:
Method 1: Program to Check Prime Number Using For loop
import java.util.*; // wildcard Scanner import public class PrimeNumber { public static void main(String[] args) { int num; boolean isPrime = true; Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); num = input.nextInt(); input.close(); for(int c = 2; c <= num/2; c++) { if(num % c == 0) { isPrime = false; break; } } if(isPrime) System.out.println(num + " is a prime number."); else System.out.println(num + " is not a prime number."); } }
Explanation:
The program declares a boolean variable isPrime to store whether the input number is a prime number and an integer variable num to store the user input. It then prompts the user to enter a number and creates a Scanner object to receive user input from the console. The nextInt() method of the Scanner class is used to read the integer input, which is then assigned to the variable num.
The for loop iterates up to num/2 starting from 2, and determines if the loop variable c is divisible by number. The break statement is used to end the loop. The program then checks the value of isPrime after the loop and if it is accurate, a message is printed to the console indicating that the number is a prime. If it is untrue, a different message is printed.
Output:
Enter a number:
23
23 is a prime number.
Get complete Java Programming Exercises and Solutions here!
Method 2: Program to Check Prime Number Using While loop
import java.util.*; // wildcard Scanner import public class PrimeNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number:"); int n = sc.nextInt(); int i = 2; boolean flag = false; while (i <= n / 2) { // condition for nonprime number if (n % i == 0) { flag = true; break; } ++i; } if (!flag) System.out.println(n + " is a prime number."); else System.out.println(n + " is not a prime number."); } }
Explanation:
This method is similar to the previous one, except that we use a while loop instead of a for loop.
We initialize the variable i to 2 and use it as the loop variable.
The loop continues until i is less than or equal to n/2.
If the number is divisible by any number other than 1 and itself, then it is not a prime number.
We set the flag to true and break out of the loop.
If the flag is false, it means that the number is a prime number, and we print the message accordingly.
Output:
Enter a number:
13
13 is a prime number
Learn How to Generate Random Numbers in Javascript, Now!
Tips and tricks to write a Prime Number Program in Java
Here are some tips and tricks to help you write more efficient prime number programs in Java:
- Use the square root of the number instead of the number itself as the upper limit of the loop. This reduces the number of iterations required in the loop, making the program more efficient.
- Use a boolean flag to keep track of whether the number is prime or not. This saves unnecessary iterations in the loop.
- Start the loop at 2 instead of 1, as 1 is not a prime number.
- If you need to find multiple prime numbers, you can use a nested loop to iterate over a range of numbers.
In this blog, we learned what prime numbers are and how to write a Java program to find them. We also provided some useful tips and tricks to help you write more efficient prime number programs. Remember to always optimize your code and make use of the available tools to improve performance.
We hope that our blog post on “Prime Number Program in Java” was informative to you and answer your Java-related queries.To keep enhancing your coding skills, do check out Newtum’s website and explore our online coding courses covering Java,html, PHP, and more.