k.jahangir kumar
javaprograms
1 Answer(s)      a year and 7 months ago
Posted in : Java Beginners

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

View Answers

October 17, 2011 at 11:23 AM


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);
                      }
                    }
                   }
                }
        }
}









Related Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.