Question

Write a function to reverse an integer using numeric operators and without using any arrays or...

  1. Write a function to reverse an integer using numeric operators and without using any arrays or other data structures.
    The signature of the function is:
    int f(int n)

Examples

if the input integer is return
1234 4321
12005 50021
1 1
1000 1
0 0
-12345 -54321

by java

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class ReverseIntegers {

    public int f(int n) {
        int sign = 1;
        if (n < 0) {
            sign = -1;
            n = -1 * n;
        }
        int rev = 0;
        while (n > 0) {
            rev *= 10;
            rev += n % 10;
            n /= 10;
        }
        return sign * rev;
    }

    public static void main(String[] args) {
        System.out.println(new ReverseIntegers().f(1234));
        System.out.println(new ReverseIntegers().f(12005));
        System.out.println(new ReverseIntegers().f(1));
        System.out.println(new ReverseIntegers().f(1000));
        System.out.println(new ReverseIntegers().f(0));
        System.out.println(new ReverseIntegers().f(-12345));
    }
}

Add a comment
Know the answer?
Add Answer to:
Write a function to reverse an integer using numeric operators and without using any arrays or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT