Sunday, 9 July 2017

Write a program to reverse a number.


Description:
Write a program to reverse a number using numeric operations. Below example shows how to reverse a number using numeric operations.

Code:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.govindsblog.algos;
 
public class NumberReverse {
 
    public int reverseNumber(int number){
         
        int reverse = 0;
        while(number != 0){
            reverse = (reverse*10)+(number%10);
            number = number/10;
        }
        return reverse;
    }
     
    public static void main(String a[]){
        NumberReverse nr = new NumberReverse();
        System.out.println("Result: "+nr.reverseNumber(17868));
    }
}

Output:
Result: 86871

No comments:

Post a Comment