Jessica M.
Jessica M.

Reputation: 1459

How to write a program that reverses a number a user inputs

The homework problem is the user enters a number. Then you have to write a program that reverses that order. So if the user enters 7364 you have to write a program that presents 4637 on the next line. I think I've figured out the solution but I'm not sure how to write it.

Since the last number is the first number in reverse order that means that if someone enters 7364 that means i want to get 4637. I have to write a program that multiplies 4 by 1000, 6 by 100, 3 by 10 and 7 by 1 then add those up to get 4637. I'm not not 100% sure how to do it. What's messing me up is how to multiply one number by 1000, the next by 100, the next by 10 and the next by 1 then add those up.

import acm.program.*;
public class ReverseNumber extends ConsoleProgram{
    public void run(){

    int n = readInt("please enter any positive number: ");

    int total = 0;


    while ( n > 0){
    total = total + n % 10;    <----?
    n = n * 1000;         <----?
      }
    println("the reverse order is" + total); 

   }

  }

Upvotes: 1

Views: 4058

Answers (7)

TheGAme
TheGAme

Reputation: 1

public class value{

   public static void main(String[] args){

    int n=Integer.parseInt(args[0]);
    int t=0;

    do{
        t=n%10;
        System.out.print(t);
        n=n/10;
    }while(n>0);


}
}

Upvotes: 0

Alex Ciminian
Alex Ciminian

Reputation: 11508

This should work:

    total = 0;
    while (n > 0) {
        total = total * 10 + n % 10;
        n = n / 10;
    }
    println("the reverse order is " + total); 

You don't have to know how many digits there are in the original number, you're iterating through all of them anyway. Here's what happens:

  • When you get a new digit (n % 10), you multiply the result by 10 and add it to it. This way, you offset the digits in the result.
  • Then you eliminate the last digit (the one you added in the step before) from the original number by doing n / 10.

Upvotes: 1

RisingSun
RisingSun

Reputation: 1733

Well, to reverse the number the simplest solution would be to convert it to a string get the first letter and append it at the end until you reach the last letter or number in this case. Also, you can do pretty much the same with the multiplication part. Get the numbers one by one as a string convert it back to int then multiply and add.

EDIT: if you cant do it using strings. here is a somewhat mathematical solution.

int num = 123456; // any number than you want to reverse
string revnum = ''; // the reversed number
int temp = 0;
do {
 temp= (temp*10)+(num%10);
 num = (int)(num/10);
}while(num>0){
 revnum = revnum + temp;
}

Upvotes: 2

Azodious
Azodious

Reputation: 13872

Try this:

while( n != 0 )
{
    reverse = reverse * 10;
    reverse = reverse + n%10;
    n = n/10;
}

Logic is to get a single digit in each iteration starting from unit place, until all digits are encountered.

n is the input no. reverse is the variable where reverse of n is stored after while is finished.

% operator when used with 10, gives you the digit at unit place. / operator when used with 10, goves you all the digits except the digit at unit place.

When n = 7364 and reverse = 0

in 1st iteration, loop will look like:

while(7364 != 0) // true
{
    reverse = 0 * 10; // reverse = 0
    reverse = 0 + 7364%10 // reverse = 4
    n = 7364/10 // n = 736
}

in 2nd iteration:

while(736 != 0) // true
{
    reverse = 4 * 10; // reverse = 40
    reverse = 40 + 736%10 // reverse = 46
    n = 736/10 // n = 73
}

in 3rd iteration:

while(73 != 0) // true
{
    reverse = 46 * 10; // reverse = 460
    reverse = 460 + 73%10 // reverse = 463
    n = 73/10 // n = 7
}

in 4th iteration:

while(7 != 0) // true
{
    reverse = 463 * 10; // reverse = 4630
    reverse = 4630 + 7%10 // reverse = 4637
    n = 7/10 // n = 0
}

in 5th iteration:

while(0 != 0) // false and loop ends
{
    ...
}

and we have reverse = 4637.

Upvotes: 2

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

The easiest way to do it using library.

System.out.println(new StringBuilder(String.valueOf(i)).reverse());

Upvotes: 3

Russell Gutierrez
Russell Gutierrez

Reputation: 1385

Convert the Int to String, then put it in a StringBuffer then use .reverse()

I wouldn't want to add the codes because there are many samples for this. Like this one.

After that, you could convert it again to String.

Upvotes: 0

Daniel Persson
Daniel Persson

Reputation: 2337

Do you have to represent it with an int? A String seems more natural?

If you stick with the int, you need to keep track of the factor to multiply with: which means another variable that you multiply by 10 each iteration.

Upvotes: 0

Related Questions