Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Need to reduce an integer in ascending/descending Order [ simple steps? ]

1. ## 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.

Thanks.

2. ## Re: Need to reduce an integer in ascending/descending Order [ simple steps? ]

Originally Posted by Maxfmc
...For example, the number 12345 will need to reduce in descending order 1234, 123, and so on.
Let me make sure I understand the assignment.

Suppose the number is 123456789. MIght a program run look like the following?

Enter an integer: 123456789
123456789
12345678
1234567
123456
12345
1234
123
12
1

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

Also
12345678/10 = 1234567

Etc.

Originally Posted by Maxfmc
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.
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.")

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
102030405
10203040
1020304
102030
10203
1020
102
10
1

As before, I note that, using integer arithmetic on integer variables:

102030405/10 = 10203040

Also
10203040/10 = 1020304

Etc.

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
12345
1234
123
12
1

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.)

Anyhow...

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.

Cheers!

Z