44

Can anyone explain to me how to reverse an integer without using array or String. I got this code from online, but not really understand why + input % 10 and divide again.

while (input != 0) {
    reversedNum = reversedNum * 10 + input % 10;
    input = input / 10;   
}

And how to do use this sample code to reverse only odd number. Example I got this input 12345, then it will reverse the odd number to output 531.

0

34 Answers 34

1
2
-1
public static double reverse(int num)
{
    double num1 = num;
    double ret = 0;
    double counter = 0;

    while (num1 > 1)
    {   
        counter++;
        num1 = num1/10;
    }
    while(counter >= 0)
    {
        int lastdigit = num%10;
        ret += Math.pow(10, counter-1) * lastdigit;
        num = num/10;
        counter--;  
    }
    return ret;
}
Sign up to request clarification or add additional context in comments.

Comments

-1
import java.util.Scanner;

public class ReverseOfInteger {
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        int x = input.nextInt();
        System.out.print(helpermethod(x));
    }

    public static String helpermethod(int x) {
        if (x == 0)
            return "";
        String a = String.valueOf(x % 10);
        return a + helpermethod(x / 10);

    }
}

Comments

-1

I used String and I converted initially the int to String.Then I used the reverse method. I found the reverse of the number in String and then I converted the string back to int. Here is the program.

import java.util.*;

public class Panathinaikos {
    public static void my_try()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the number you want to be reversed");
        int number = input.nextInt();
        String sReverse = Integer.toString(number);
        String reverse = new StringBuffer(sReverse).reverse().toString();
        int Reversed = Integer.parseInt(reverse);
        System.out.print("The number " + number+ " reversed is " + Reversed);
    }
}

2 Comments

That works but I think the questioner wanted a solution that didn't involve strings.
ohhh yes!sorry i didn't see it
-1
public static void reverse(int number) {
    while (number != 0) {
        int remainder = number % 10;
        System.out.print(remainder);
        number = number / 10;
    }

    System.out.println();
}

What this does is, strip the last digit (within the 10s place) and add it to the front and then divides the number by 10, removing the last digit.

1 Comment

While this code may answer the question, it would be better to explain how it solves the problem without introducing others and why to use it. Code-only answers are not useful in the long run.
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.