Write a class PrimeFinder to accept the number and find it is a prime number or not? Using control structures

0

Assignment: Module 03- Part 02

Write a class PrimeFinder to accept the number and find it is a prime number or not? Using control structures

Write a class PrimeFinder to accept the number and find it is a prime number or not? Using control structures

Input: 

Enter the number 4

Output: 

The given no is not a prime

Input: 

Enter the number 17

Output: 

The given no is prime

Here's one possible implementation in Java:

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

    if (num <= 1) {
      System.out.println("The given no is not a prime");
    } else {
      boolean isPrime = true;
      for (int i = 2; i <= num / 2; i++) {
        if (num % i == 0) {
          isPrime = false;
          break;
        }
      }
      if (isPrime) {
        System.out.println("The given no is prime");
      } else {
        System.out.println("The given no is not a prime");
      }
    }
  }
}   

👉Java Fundamentals Assignments👈

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
✨ Updates