java code for prime number
- User
java code for prime number
- Assistant
To determine if a number is prime in Java, you can use the following code:
javapublic class PrimeNumber { // Method to check if a number is prime public static boolean isPrime(int number) { // Corner cases - for numbers less than or equal to 1 if (number <= 1) { return false; } // Check from 2 to the square root of the number for (int i = 2; i <= Math.sqrt(number); i++) { if (number % i == 0) { return false; } } return true; } public static void main(String[] args) { int num = 29; if (isPrime(num)) { System.out.println(num + " is a prime number."); } else { System.out.println(num + " is not a prime number."); } } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.