In a file called Decomposition.java, write a program that reads a positive numbe
ID: 3633449 • Letter: I
Question
In a file called Decomposition.java, write a program that reads a positive number from the user and outputs it as a combination of powers of 10 (which corresponds to the number of ones (10^0), tens (10^1), hundreds (10^2)… that composes it).The program’s output should look as follows (user input is bold):
Please enter a number between 0 and 9999: 1304
1304 = 4*10^0 + 0*10^1 + 3*10^2 + 1*10^3
Hint:
? Notice that 1234 / 10 = 123 and 1234 % 10 = 4
? The program should use a while loop which stops when there are no more digits to look for.
Explanation / Answer
Here's mine: import java.util.Scanner; public class hw2 { /** * @param args */ public static void main(String[] args) { long exponent, value, digit; Scanner kbd = new Scanner(System.in); System.out.println ("Enter a value: "); value = kbd.nextInt (); exponent = 0; while (value > 0) { digit = value % 10; value = value / 10; System.out.println ("" + digit + "*10^" + exponent); exponent++; } } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.