Ask your own question, for FREE!
Mathematics 16 Online
OpenStudy (calculusxy):

Write a program that asks the user to enter a two-digit number, then prints the number with its digits reversed. Do the problem with a second approach that makes use of the division and modulus operators

OpenStudy (calculusxy):

@phi

OpenStudy (phi):

One way is read in a string, and print out the string in reverse order the 2nd way, we first need the algorithm... did you figure it out ?

OpenStudy (calculusxy):

No not really. I have been trying to look out other places for reference, but I don't understand what they are saying.

OpenStudy (calculusxy):

public void reverse(int n) { int result = 0; int rem; while (n > 0) { rem = n % 10; n = n / 10; result = result * 10 + rem; } }

OpenStudy (calculusxy):

Will these line of commands get it? I got this from the website: http://www.javawithus.com/programs/reversing-a-number

OpenStudy (phi):

yes. For only 2 digits you can probably simplify it a bit.

OpenStudy (calculusxy):

Can you explain how it will work?

OpenStudy (calculusxy):

With an example?

OpenStudy (calculusxy):

@phi

OpenStudy (phi):

it will take me a few minutes.

OpenStudy (calculusxy):

ok

OpenStudy (phi):

#include <stdio.h> int main() { int a, b; while (1) { printf("Enter a number (0 to exit): "); scanf("%2d", &a); if (a==0) break; b= (a % 10)*10+(a/10); printf("original %2d reversed: %2d\n", a,b); } }

OpenStudy (calculusxy):

I am still unsure about why you would want to divide by 10 and then multiply and then add it to the quotient of a/10

OpenStudy (calculusxy):

I just can't process what the mathematical things represent or mean

OpenStudy (phi):

the operator % returns the "remainder" example: 23 % 10 returns 3 (the remainder after dividing by 10) if you do an *integer divided* i.e. divide integers (not floats), C returns only the whole number. example 23/10 = 2 once we have those 2 digits we want 3*10 + 2 to reverse the 23 to get 32

OpenStudy (calculusxy):

This makes so much more sense! Thank you soooo.. much!

OpenStudy (calculusxy):

Another question (sorry) do I need to use the while loop?

OpenStudy (phi):

not really, but the program will only run once. If you want to test out various cases, it is more convenient to have it loop. There is one "bug" that you could tweak: enter 10, the program will print 1 it will leave off the leading 0 ... it might look better to type out 01. If you want that, you have to fix it somehow. maybe do a test, and use a different print statement.

OpenStudy (calculusxy):

I am writing the program now.. So I will have `int digits;` `int last_digit;` `int first_digit;` `int reverse;`

OpenStudy (phi):

ok. The program I posted works, so if you run into trouble, you can use it to debug your work. good luck.

OpenStudy (calculusxy):

It works!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!