Using Recursion to reverse an integer without the use of strings

后端 未结 5 447
逝去的感伤
逝去的感伤 2020-12-20 10:22

I have been trying this for some time now but could not get it to work. I am trying to have a method to reverse an integer without the use of strings or arrays. For example,

5条回答
  •  感情败类
    2020-12-20 10:55

    package Test;
    
    public class Recursive {
        int i=1;
        int multiple=10;
        int reqnum=0;
        public int recur(int no){
            int reminder, revno;
    
            if (no/10==0) {reqnum=no;
            System.out.println(" reqnum "+reqnum);
            return reqnum;}
            reminder=no%10;
            //multiple =multiple * 10;
            System.out.println(i+" i multiple "+multiple+" Reminder "+reminder+" no "+no+" reqnum "+reqnum);
            i++;
    
            no=recur(no/10);
            reqnum=reqnum+(reminder*multiple);
            multiple =multiple * 10;
            System.out.println(i+" i multiple "+multiple+" Reminder "+reminder+" no "+no+" reqnum "+reqnum);
            return reqnum;
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int num=123456789;
    
            Recursive r= new Recursive();
            System.out.println(r.recur(num));
        }
    
    }
    

提交回复
热议问题