Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


k.jahangir kumar
javaprograms
1 Answer(s)      2 years and 9 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


Advertisement
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 Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com