My new problem is based on the code below. The code was written in Chapter 4 and allows a user to enter 3 numbers, (or a 3 digit number, however you want to look at it) and then prints the numbers in reverse order, ie 123 becomes 321.
The current problem in Chapter 6 reads as follows.
Generalize the program so that the number can have 1, 2, 3, 4 or more digits. Hint: Use a do loop that repeatedly divides the number by 10, stopping when it reaches 0.
I have several problems that are on my mind.
1) The current code is setup to accept exactly what I want the user to enter, so how can I possibly write the code to accept a number of any amount of digits.
example: Below, for a 3 digit number, we do this..
I'd have to write something like ((userNumber/100) % 10) / 10 for the 4th number, then something for the 5th digit, 6th digit, etc. etc, it could get very long and ugly.
2) Since the above "formulas" are how we reverse the numbers, what does the books suggestion of "repeatedly divide the userNumber by 10, stopping when it reaches 0", have to do with this? If the user enters 4000. Then we divide it down to 0, what does that get me? It doesn't reverse the numbers. It's obviously loop behavior, but for what end?
Originally when I was thinking about this on paper, I wrote
printf (Enter a number)
scanf (%d, userNumber)
do
(divide userNumber / 10)
while (userNumber > 0 ) //so that's my loop, I think it's what the book wants
So this will all end in 0, but what about the number reversal? And how can I use this loop to get that? I know I can only use the "formulas"(referenced above from my 3 digit example) if I know in advance how many digits the user is expected to enter. So that's out of the question, right? I'm stuck thinking about this divide by 10 business, I see how it works for the loop to keep looping and terminate when 0 has been reached, but how does it get my numbers reversed?
Can anybody give me a solid clue here? Get me thinking in the right direction?
Again I'm thinking, okay we want to enter a number and reverse it.
The only way I know how is to do......
Say, user enters 100000
then 100000/10 = 10000
then 10000/10 = 1000
then 1000/10 = 100
then 100/10 = 10
then 10/10 = 1
then 1/10 = and so on.. I can do this with a loop, but where does my result come from?
I tried to look at this from a more simple perspective also, assuming the user only enters 2 numbers, then how will / 10 reverse the numbers I asked myself.
So, 58/10 = 5
5/10 < 0 , so I can use this to tell me there are two digits.
Likewise
102 / 10 =10
10/10 =1
1/10 < 0, so I can use this to see that there are 3 digits.
But none of these give me anything I can use to reverse the numbers.
The current problem in Chapter 6 reads as follows.
Generalize the program so that the number can have 1, 2, 3, 4 or more digits. Hint: Use a do loop that repeatedly divides the number by 10, stopping when it reaches 0.
I have several problems that are on my mind.
1) The current code is setup to accept exactly what I want the user to enter, so how can I possibly write the code to accept a number of any amount of digits.
example: Below, for a 3 digit number, we do this..
Code:
printf ("The reversal is: %d%d%d\n", userNumber % 10, (userNumber / 10) % 10, userNumber / 100)
2) Since the above "formulas" are how we reverse the numbers, what does the books suggestion of "repeatedly divide the userNumber by 10, stopping when it reaches 0", have to do with this? If the user enters 4000. Then we divide it down to 0, what does that get me? It doesn't reverse the numbers. It's obviously loop behavior, but for what end?
Originally when I was thinking about this on paper, I wrote
printf (Enter a number)
scanf (%d, userNumber)
do
(divide userNumber / 10)
while (userNumber > 0 ) //so that's my loop, I think it's what the book wants
So this will all end in 0, but what about the number reversal? And how can I use this loop to get that? I know I can only use the "formulas"(referenced above from my 3 digit example) if I know in advance how many digits the user is expected to enter. So that's out of the question, right? I'm stuck thinking about this divide by 10 business, I see how it works for the loop to keep looping and terminate when 0 has been reached, but how does it get my numbers reversed?
Can anybody give me a solid clue here? Get me thinking in the right direction?
Again I'm thinking, okay we want to enter a number and reverse it.
The only way I know how is to do......
Code:
("The reversal is: %d%d%d\n", un % 10, (un / 10) % 10, un / 100)
Say, user enters 100000
then 100000/10 = 10000
then 10000/10 = 1000
then 1000/10 = 100
then 100/10 = 10
then 10/10 = 1
then 1/10 = and so on.. I can do this with a loop, but where does my result come from?
I tried to look at this from a more simple perspective also, assuming the user only enters 2 numbers, then how will / 10 reverse the numbers I asked myself.
So, 58/10 = 5
5/10 < 0 , so I can use this to tell me there are two digits.
Likewise
102 / 10 =10
10/10 =1
1/10 < 0, so I can use this to see that there are 3 digits.
But none of these give me anything I can use to reverse the numbers.
Code:
#include <stdio.h>
main ()
{
int userNumber ;
printf ("Enter a 3 digit number: ") ;
scanf ("%d", &userNumber) ;
printf ("The reversal is: %d%d%d\n", un % 10, (un / 10) % 10, un / 100) ;
return 0 ;
}