Need to reduce an integer in ascending/descending Order [ simple steps? ]
Yes, it's related to homework. I need help with a program that can reduce itself. For example, the number 12345 will need to reduce in descending order 1234, 123, and so on. But I've been warned that if I include the number 0 to the number 12345 the program will not execute properly even though it will work. I figured that I can use the mod function [divide by 10,100,1000,...] to decrease the number and only display the remaining number.
I want someone's opinion on how is the best possible way to write/start the program. I would appreciate a very simple example of such a program or details on how I can start. I should mention I'm a beginner and know basic functions related to Java.
Re: Need to reduce an integer in ascending/descending Order [ simple steps? ]
Let me make sure I understand the assignment.
Originally Posted by Maxfmc
Suppose the number is 123456789. MIght a program run look like the following?
Enter an integer: 123456789
Well, how would a program produce that sequence of numbers from the initial one?
I note that, using integers for the variables, according to the rules of integer arithmetic
123456789/10 = 12345678
12345678/10 = 1234567
What the heck does that mean? If it doesn't execute properly, how can anyone say that it "will work?" Either it works or it doesn't work. If it "works," then that means it executes properly, right? What am I missing here? (How about getting the person who warned you give an example for which he/she thinks that, "it will not execute properly even though it will work.")
Originally Posted by Maxfmc
Anyhow, my next question is: What does having a zero digit in the number have to do with anything?
For example, suppose the number is 102030405. Then a run might look like the following:
Enter an integer: 102030405
As before, I note that, using integer arithmetic on integer variables:
102030405/10 = 10203040
10203040/10 = 1020304
Now, if you had a "leading zero" the simplest kind of Java numerical print function might not show it, so a run could look like
Enter an integer: 012345
But so what? I mean, are you required to print the leading zero each time? Or is that program output OK? Or what? (I'll assume that output is OK unless you clarify the requirements to explain something more about it.)
I don't know whether there is a "best way" to do anything, but if you had some kind of loop that prints out the value of the number and then divides by 10 each time through the loop, it could show output sequences like my examples. (Eventually you can figure that repeatedly dividing by 10 will result in a quotient whose value is zero. That's how you know when to terminate the loop.)
I'm not sure what the modulo operator has to do with anything in the context of my interpretation of the assignment.
But it's entirely possible that I missed something (again).
If I have misunderstood the assignment, maybe you can enlighten me.