For a given integer n > 1, the smallest integer d > 1 that divides n is a prime factor. We can find the prime factorization of n if we find d and then replace n by the quotient of n divided by d, repeating this until n becomes 1. Write a java program that uses a stack to print the prime factors of a positive integer in descending order. For example, for n = 3960, your program should produce 11*5*3*3*2*2*2.

A stack can be used to print numbers in other bases (multibase output). Examples:

  • (Base8) 28 base 10 = (3 * 8^1) + (4 * 8^0) = 34 base 8
  • (Base4) 72 base 10 = (1 * 4^3) + (0 * 4^2) + (2 * 4^1) + (0 * 4) = 1020 base 4
  • (Base 2) 53 base 10 = (1 * 2^5) + (1 * 2 ^ 4) + (0 * 2^3) + (1 * 2^2) + (0 * 2^1) + (1 * 2^0) = 110101 base 2

Write a java program using stacks that takes 3 non-negative (base 10) long integer numbers and a base B (B is in the range 2-9) end writes the number to the screen as a base B number. The program prompts the user for 3 numbers and bases, and then outputs them.

Use as input:

  • 72 base 10 to base 4
  • 53 base 10 to base 2
  • 3553 base 10 to base 8
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.