Ads

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

Ads

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

Ads

Related Tutorials/Questions & Answers:

Ads

- Java Tutorials
- Java Code example
- Java Programming
- Java Beginners Examples
- Applet Tutorials
- Awt Tutorials
- Java Certification
- Interview Question
- Java Servlets Tutorial
- Jsp Tutorials
- Java Swing Tutorials
- JDBC Tutorial
- EJB Tutorials
- Java Server Faces (JSF) Tutorial
- WAP Tutorial
- Struts Tutorial
- JAXB Tutorial
- Spring FrameWork Tutorial
- SOA&Web Services Tutorials
- Bioinformatics Tutorials
- MySQL Tutorials
- JAVA DOM Tutorial
- XML Tutorial
- EAI Articles
- Many Programming Tutorials Links
- Tutorials Books
**Java Script Tutorial****Ajax Tutorial****Dojo Tutorials****Programming Books****Trainings****Flex****Ant****RDF**

Ads