Sunday, October 12, 2014

Reverse Number


// how to write program: 
// give an example
// identify number by variable
// convert to program
// verify program by example

// if number to reverse is 123
// 12 3     0x10+3
// 1   2     3x10+2
// 0   1     32x10+1

public class ReverseNumberTest {
   
    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[] args){
        ReverseNumberTest rnt = new ReverseNumberTest();
        System.out.println("reverse number="+rnt.reverseNumber(123));
    }

}

No comments:

Post a Comment