A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. ( Java programing )
//PrimeNumberCheck.java import java.util.Scanner; public class PrimeNumberCheck { private static boolean isPrime(int n) { if(n%2!=0 && n%3!=0){ return true; } else{ return false; } } public static void main(String args[]) { int n; Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); n = scanner.nextInt(); if(isPrime(n)){ System.out.println(n+" is a prime"); } else{ System.out.println(n+" is a not prime"); } } }
Get Answers For Free
Most questions answered within 1 hours.