reverse the position of integer digits?

后端 未结 13 2518
予麋鹿
予麋鹿 2021-02-09 15:00

i have to reverse the position of integer like this

input = 12345

output = 54321

i made this but it gives wrong output e.g 5432

#include         


        
13条回答
  •  情书的邮戳
    2021-02-09 15:37

    public class TestDS {

    public static void main(String[] args) {
    
        System.out.println(recursiveReverse(234));
               System.out.println(recursiveReverse(234 ,0));
    
    
    
    }
    
    
    
    public static int reverse(int number){
    
        int reversedNumber = 0;
        int temp = 0;
    
    
        while(number > 0){
    
            //use modulus operator to strip off the last digit
            temp = number%10;
    
            //create the reversed number
            reversedNumber = reversedNumber * 10 + temp;
            number = number/10;
    
        }
    
    
    
        return reversedNumber;
    
    }
    
    
    
    private static int reversenumber =0;
    public static int recursiveReverse(int number){
    
        if(number <= 0){
    
            return reversenumber;
        }
    
        reversenumber = reversenumber*10+(number%10);
        number =number/10;
    
        return recursiveReverse(number);
    
    }
    
    public static int recursiveReverse(int number , int reversenumber){
    
        if(number <= 0){
    
            return reversenumber;
        }
    
        reversenumber = reversenumber*10+(number%10);
        number =number/10;
    
        return recursiveReverse(number,reversenumber);
    
    }
    

    }

提交回复
热议问题