Prime Number Program in Java

# Prime Number Program in Java

Prime numbers in Java: A prime number is any number greater than 1 that can only be divided evenly by 1 and itself. This means that prime numbers do not have divisors other than 1 and their own value. Examples of prime numbers include 2, 3, 5, 7, 11, 13, 17, and so on.

Fill Out the Form for Expert Academic Guidance!

+91

Live ClassesBooksTest SeriesSelf Learning

Verify OTP Code (required)

Also Check: Fibonacci Series in Java | Computer | Cache Memory

## Methods to Write Prime Number Program in Java

To determine if a number is prime in Java, there isn’t a specific formula, but several methods can be used. Below are some approaches to find out whether a number is prime:

import java.util.Scanner;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number to check if it's prime: ");
int number = scanner.nextInt();
scanner.close();

if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}

// Function to check if a number is prime
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}



## Prime Number Program in Java Using Scanner

import java.util.Scanner;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
scanner.close();

boolean isPrime = true;

if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}


## Prime Number Program in Java Using loop

import java.util.Scanner;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
scanner.close();

boolean isPrime = true;

if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}


## Prime Number Program in Java Using While loop

import java.util.Scanner;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int i = 2;
boolean isPrime = true;

while (i <= number / 2) {
if (number % i == 0) {
isPrime = false;
break;
}
i++;
}

if (isPrime && number > 1) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
scanner.close();
}
}


## Prime Number Program in Java Using if else

import java.util.Scanner;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
scanner.close();

boolean isPrime = true;

if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}


## Prime Number Program in Java FAQs

### How to find prime numbers in Java using if else?

Check if a number is divisible only by 1 and itself using if-else conditions.

### What is the formula for prime numbers in Java?

There isn't a single formula, but you can use loops to check divisibility and determine prime numbers.

### How to check if a number is prime Java without using function?

Use nested loops to check if the number is divisible by any other number except 1 and itself.

### How to find prime number in Java using for loop?

Iterate through numbers and use a for loop to check for prime numbers.

### How to find prime numbers using for loop in Java?

Use a for loop to iterate through numbers and check if each one is prime.

### How to check for prime numbers in Java?

Use a loop to check if a number is divisible only by 1 and itself.

### What is the loop for prime numbers?

Use a for loop to iterate through numbers and check for primes.

### How do you print prime numbers from 1 to 100 using for loop?

Iterate from 1 to 100 using a for loop, and check each number for primality before printing it.

## Related content

 Data Scientist Roadmap 2024 Fibonacci Series in Java What is Database? What is Binary Code? – Definition, Advantages What is HTTP? – (HyperText Transfer Protocol) USB (Universal Serial Bus) – History, Definition, Invention, Uses, & Facts Telephone – History, Uses, Facts and More What is an IP Address – Definition, Explanation, How its works System Software – Features, Types, Issues, Difference b/w System Software And Application Software Barcodes Defined – How They Work, Benefits & Uses

+91

Live ClassesBooksTest SeriesSelf Learning

Verify OTP Code (required)