user2130057
user2130057

Reputation: 406

Reversing an integer in Java using a for loop

This is a homework problem

How would I reverse an integer in Java with a for loop? The user will input the integer (I don't know how long it will be) and I need to reverse it. ie: If they enter 12345, my program returns 54321.

Here's the catch, you can't use String, StringBuffer, arrays, or other advanced structures in this problem.

I have a basic idea of what I need to do. My problem is...in the for loop, wouldn't the condition need to be x < the length of the integer (number of digits)? How would I do that without String?

Thanks for any input, and I'll add more information if requested.

EDIT:

Of course, after introspection, I realized I should use another for loop to do this. What I did was create a for loop that will count the digits by dividing by 10:

       int input = scan.nextInt();
        int n = input;
        int a = 0;

           for (int x = 0; n > 0; x++){
               n = n/10;
                a = a + 1;
            }

EDIT 2:

This is what I have

 int input = scan.nextInt();
        int n = input;
        int a = 0;
        int r = 0;
           for (int x = 0; n > 0; x++){
               n = n/10;
                a = a + 1;
            }
           for (int y = 0; y < n; y++) { 
                r = r + input%10;
                input = input/10;
            }
          System.out.println(input);

When I run it, it isn't reversing it, it's only giving me back the numbers. ie: if I put in 1234, it returns 1234. This doesn't make any sense to me, because I'm adding the last digit to of the input to r, so why wouldn't it be 4321?

Upvotes: 1

Views: 11081

Answers (5)

Karan shah
Karan shah

Reputation: 175

here is the Answer With Correction of Your Code.

import static java.lang.Math.pow;
import java.util.*;
public class MyClass {
public static void main(String args[]) {
    Scanner scan=new Scanner(System.in);
    int input = scan.nextInt();
    int n = input;
    int a = 0;
    int r = 0;
       for (; n > 0;){
           n = n/10;
            a = a + 1;
        }
       for (int y = 0; y < input;a--) { 
            r =(int)( r + input%10*pow(10,a-1));
            input = input/10;
        }
      System.out.println(r);
}
}

Upvotes: 0

user2778084
user2778084

Reputation:

    public static void reverse2(int n){
    int a;


    for(int i = 0; i < n  ; i ++){
        a = n % 10;
        System.out.print(a);
        n = n / 10;


        if( n < 10){
            System.out.print(n);
            n = 0;
        }
        }

    }

Upvotes: 0

cHao
cHao

Reputation: 86575

While your original number is nonzero, take your result, multiply it by 10, and add the remainder from dividing the original by 10.

For example, say your original number is 12345. Start with a result of 0.

  1. Multiply result by 10 and add 5, giving you 5. (original is now 1234.)
  2. Multiply result by 10 and add 4, giving you 54. (original is now 123.)
  3. Multiply result by 10 and add 3, giving you 543. (original = 12.)
  4. Multiply result blah blah 5432. (original = 1.)
  5. Multiply, add, bam. 54321. And 1 / 10, in int math, is zero. We're done.

Your mission, should you choose to accept it, is to implement this in Java. :) (Hint: division and remainder are separate operations in Java. % is the remainder operator, and / is the division operator. Take the remainder separately, then divide the original by 10.)

Upvotes: 6

user1709694
user1709694

Reputation: 322

This might not be the proper way but

   public static int reverseMe(int i){

    int output;
    String ri = i + "";

    char[] inputArray = ri.toCharArray();
    char[] outputArray = new char[inputArray.length];

    for(int m=0;m<inputArray.length;m++){
        outputArray[inputArray.length-m-1]=inputArray[m];
    }

    String result = new String(outputArray);
    output = Integer.parseInt(result);

    return output;
}

Upvotes: 0

rgettman
rgettman

Reputation: 178333

You will need to use math to access each of the digits. Here's a few hints to get your started:

Use the % mod operator to extract the last digit of the number. Use the / division operator to remove the last digit of the number. Stop your loop when you have no more digits in the number.

Upvotes: 5

Related Questions