angel sanchez
angel656
1 Answer(s)      5 years and 7 months ago
Posted in : Java Beginners

write a method that tales an integer value and returns the number with its digit reverse.For Example given the number 7631 the method should return 1367

Ads

View Answers

January 19, 2011 at 4:52 PM


Hi Friend,

Try this:

import java.util.*;

public class ReverseNumber {
    public static void main(String[] args) {
        try {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter number: ");
            int num = input.nextInt();
            int n = num;
            int reversedNumber = 0;
            for (int i = 0; i <= num; i++) {
                int r = num % 10;
                num = num / 10;
                reversedNumber = reversedNumber * 10 + r;
                i = 0;
            }
            System.out.print("After reversing, number be: "+reversedNumber);
        }
             catch (Exception e) {
            System.out.println(e);
        }
    }
}

Thanks










Related Tutorials/Questions & Answers:
angel656
angel656  write a method that tales an integer value and returns the number with its digit reverse.For Example given the number 7631 the method should return 1367

Ads

Advertisement