Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

reversing an integer with overflow handled

// Java program to reverse digits
// of a number
public class GFG
{
 
  static int reversDigits(int num)
  {
    int rev = 0  ;
 
    while(num != 0){       
      int rem = num % 10 ;
      num /= 10 ;
 
      if(rev > Integer.MAX_VALUE/10 || rev == Integer.MAX_VALUE/10 && rem > 7){
        return 0 ;
      }
 
      if(rev < Integer.MIN_VALUE/10 || rev == Integer.MIN_VALUE/10 && rem < -8){
        return 0 ;
      }
 
      rev = rev*10 + rem ;
    }
 
    return rev ;
  }
 
  // Driver code
  public static void main (String[] args)
  {
    int num = 12345;
    System.out.println("Reverse of no. is " + reversDigits(num) );
 
    num = 1000000045;
    System.out.println("Reverse of no. is " + reversDigits(num) );
  }
}
 
// This code is contributed by jana_sayantan.
Comment

PREVIOUS NEXT
Code Example
Java :: get time zone from co-ordinates java 
Java :: Java Another form of assertion statement 
Java :: What Is Spring Boot and What Are Its Main Features? 
Java :: using ..replace() in jShell/java 
Java :: retrofit interface 
Java :: zombie testing java 
Java :: get cursor position in textarea java 
Java :: while (rem != 0); java 
Java :: 2d matrix 
Java :: android studio analyze apk 
Java :: How to code the Fibonacci Sequence using simple iterative loops java 
Java :: franchiseRulesTemp 
Java :: metodi di javascritp 
Java :: priority queue is empty java 
Java :: Java Maven Using platform encoding (UTF-8 actually) to read mojo metadata, i.e. build is platform dependent! 
Java :: java set custom property cmd 
Java :: handle customized popup in selenium 
Java :: Kotlin Toast : error-none-of-the-following-functions-can-be-called-with-the-arguments-supplied 
Java :: grunt registertask multiple 
Java :: java static inner class 
Java :: java gson get object without class 
Java :: factorial function in java recursion 
Java :: Java how to handle HTTP GET request after establishing TCP connection 
Java :: reference to an instance method of an arbitrary object of a particular type 
Java :: android studio doesnt work when in full screen mac os 
Java :: java unfocus 
Java :: how to enable/disable wifi or internet 
Java :: Android popBackStack to specific fragment 
Java :: java web.xml 
Java :: what does % do in java 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =