Answer to Question #70366 in Java | JSP | JSF for atif haroon

Question #70366
Every positive integer greater than 1 can be expressed as a product of prime numbers. This
factorization is unique and is called the prime factorization.For example, the number 60 can be
decomposed into the factors 2 ∗ 2 ∗ 3 ∗ 5, each of which is prime. Note that the same prime can
appear more than once in the factorization. Write a program to display the prime factorization of
a number n e.g. if a user enter enter 60 as an input then program should print 2 ∗ 2 ∗ 3 ∗ 5 as an
output.
1
Expert's answer
2017-10-03T15:05:07-0400
package javaapplicationfactorization;

import java.util.Scanner;

public class JavaApplicationFactorization {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = 0;
do {
System.out.print("Enter integer greater than 1: ");
number = scan.nextInt();
}while(number <= 1);

double max = Math.floor(Math.sqrt(number));
boolean isPrime = true;

for (int i = 2; i <= max; i++) {
if (((i % 2) == 0) && (i != 2)) {
continue;
}

while(number % i == 0){
if (isPrime)
System.out.print(i);
else
System.out.print(" * " + i);

isPrime = false;
number = number / i;
}
}
if (isPrime) System.out.print(number);
System.out.println();
}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS