Sunday, January 24, 2016

Reverse of the given number using recursion in Java


public int reverse(int num) {
    if (num < 10) {
         return num;
    } else {
          int rev=num%10 * (int) Math.pow(10, (int) Math.log10(num));
          return  rev+ reverse(num / 10);
    }
}

      ------ OR --------


public int reverse(int num) {
   retrun (num < 10)? num:num%10 * (int) Math.pow(10, (int) Math.log10(num))
           +reverse(num / 10);
   }
}

 
   




No comments:

Post a Comment

Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...