javaprograms

javaprograms

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 Tutorials/Questions & Answers:
javaprograms
javaprograms
Advertisements
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms
javaprograms - Java Beginners
javaprograms - Java Beginners
javaprograms - Java Beginners

Ads