Check for integer overflow on multiplication

Given two integer a and b, find whether their product (a x b) exceed the signed 64 bit integer or not. If it exceed print Yes else print No.

Examples:

Input : a = 100, b = 200 
Output : No

Input : a = 10000000000,
        b = -10000000000
Output : Yes



Approach :

  1. If either of the number is 0, then it will never exceed the range.
  2. Else if the product of the two divided by one equals the other, then also it will be in range.
  3. In any other case overflow will occur.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to check for integer
// overflow on multiplication
#include <iostream>
using namespace std;
  
// Function to check whether there is
// overflow in a * b or not. It returns
// true if there is overflow.
bool isOverflow(long long a, long long b)
{
    // Check if either of them is zero
    if (a == 0 || b == 0) 
        return false;
      
    long long result = a * b;
    if (a == result / b)
        return false;
    else
        return true;
}
  
// Driver code
int main()
{
    long long a = 10000000000, b = -10000000000;
    if (isOverflow(a, b))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check for integer
// overflow on multiplication
import java.util.*;
import java.lang.*;
  
public class GfG{
      
    // Function to check whether there is
    // overflow in a * b or not. It 
    // returns true if there is overflow.
    static Boolean isOverflow( long a, long b)
    {
        // Check if either of them is zero
        if (a == 0 || b == 0
            return false;
      
        long result = a * b;
        if (a == result / b)
            return false;
        else
            return true;
    }
      
    // driver function
    public static void main(String argc[])
    {
        long a = Long.parseLong("10000000000");
        long b = Long.parseLong("-10000000000"); 
          
        if (isOverflow(a, b))
        System.out.print("Yes");
    else
        System.out.print("No");
    }
}
  
// This code is contributed by Prerna Saini

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to check for integer 
# overflow on multiplication 
  
# Function to check whether there is 
# overflow in a * b or not. It returns 
# true if there is overflow. 
def isOverflow(a, b): 
  
    # Check if either of them is zero 
    if (a == 0 or b == 0) :
        return False
  
    result = a * b
    if (result >= 9223372036854775807 or 
        result <= -9223372036854775808):
        result=0
    if (a == (result // b)):
        print(result // b)
        return False
    else:
        return True
  
# Driver code 
if __name__ =="__main__":
    a = 10000000000
    b = -10000000000
    if (isOverflow(a, b)):
        print( "Yes")
    else:
        print( "No")
          
# This code is contributed
# Shubham Singh(SHUBHAMSINGH10)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check for integer
// overflow on multiplication
using System;
  
public class GfG
{
      
    // Function to check whether there is
    // overflow in a * b or not. It 
    // returns true if there is overflow.
    static bool isOverflow( long a, long b)
    {
        // Check if either of them is zero
        if (a == 0 || b == 0) 
            return false;
      
        long result = a * b;
        if (a == result / b)
            return false;
        else
            return true;
    }
      
    // Driver function
    public static void Main()
    {
        long a = 10000000000;
        long b = -10000000000 ;
          
        if (isOverflow(a, b))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check for integer 
// overflow on multiplication 
  
// Function to check whether there is 
// overflow in a * b or not. It returns 
// true if there is overflow. 
function isOverflow($a, $b
    // Check if either of them is zero 
    if ($a == 0 || $b == 0) 
        return false; 
      
    $result = $a * $b
    if ($a == (int)$result / $b
        return false; 
    else
        return true; 
  
// Driver code 
$a = 10000000000;
$b = -10000000000; 
if (isOverflow($a, $b)) 
    echo "Yes"
else
    echo "No"
  
// This code is contriuted by ajit.
?>

chevron_right



Output:

Yes

Time complexity : O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.