QuestionAsk Questions?

write a proggram to accept a number and check whether the sum of prime factors of a number is a prime number or not

View Answers

April 29, 2011 at 11:50 AM

import java.util.*; class PrimeFactors { 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); long n = input.nextLong(); long sum=0; System.out.print("Sum of Prime Factors of " + n + " is: "); for (long i = 2; i <= n / i; i++) { while (n % i == 0) { sum+=i; n = n / i; } } if (n > 1){ sum=sum+n; } else { sum=sum+0; } System.out.print(sum); System.out.println(); if(isPrime(sum)){ System.out.println("Sum of prime factors of a number is a prime number"); } else{ System.out.println("Not Prime!"); } } }

Related Tutorials/Questions & Answers:

Advertisements