write a program to check whether a given number is prime number or not ,if the given number is not a prime number then print the prime factors of that number
import java.util.*; class CheckPrime { static boolean isPrime(long number) { boolean isPrime = false; int i = (int) Math.ceil(Math.sqrt(number)); while (i > 1) { if ((number != i) && (number % i == 0)) { isPrime = false; break; } else if (!isPrime) isPrime = true; --i; } return isPrime; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter Number: "); long num = input.nextLong(); if (!isPrime(num)) { System.out.println("Prime Factors are:"); for (int i = 1; i < num; i++) { if (num % i == 0) { if (isPrime(i)) { System.out.println(i); } } } } } }
Ads