Java Program - Check if a Number is Prime
Mathematics for Programming Numbers (Article) Numbers (Program)
25
This Java program checks whether a given number (in this case, 29) is a prime number. It initializes a boolean variable isPrime
to true
and uses a for
loop to iterate from 2 to number / 2
. If the number is divisible by any value in this range (number % i == 0
), it sets isPrime
to false
and breaks out of the loop. After the loop, it prints whether the number is prime based on the value of isPrime
. If isPrime
remains true
, the number is prime; otherwise, it is not.
Given Input:
29
Expected Output:
29 is a prime number.
Program:
public class PrimeCheck {
public static void main(String[] args) {
int number = 29;
boolean isPrime = true;
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.");
}
}
Output:
29 is a prime number.
Explanation:
check if any number is prime, allowing the user to input the number
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
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.");
}
}
Explanation:
- Importing
Scanner
: Imports the Scanner
class to read user input.
- Creating a Scanner Object:
Scanner scanner = new Scanner(System.in);
creates a Scanner
object to read input from the console.
- Reading User Input:
int number = scanner.nextInt();
reads an integer input from the user.
- Prime Check Logic: The code remains largely the same, but now it checks the user-provided number instead of a hardcoded value.
- Edge Case Handling: Added a condition to handle numbers less than or equal to 1, which are not prime.
This Particular section is dedicated to Programs only. If you want learn more about Mathematics for Programming. Then you can visit below links to get more depth on this subject.
29 is a prime number.
Program:
public class PrimeCheck { public static void main(String[] args) { int number = 29; boolean isPrime = true; 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."); } }
Output:
29 is a prime number.
Explanation:
check if any number is prime, allowing the user to input the number
import java.util.Scanner; public class PrimeCheck { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); 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."); } }
Explanation:
- Importing
Scanner
: Imports theScanner
class to read user input. - Creating a Scanner Object:
Scanner scanner = new Scanner(System.in);
creates aScanner
object to read input from the console. - Reading User Input:
int number = scanner.nextInt();
reads an integer input from the user. - Prime Check Logic: The code remains largely the same, but now it checks the user-provided number instead of a hardcoded value.
- Edge Case Handling: Added a condition to handle numbers less than or equal to 1, which are not prime.
This Particular section is dedicated to Programs only. If you want learn more about Mathematics for Programming. Then you can visit below links to get more depth on this subject.