Compute average of two numbers without overflow - GeeksforGeeks

28 Apr, 2021
Given two numbers, a and b. Compute the average of the two numbers.The well know formula (a + b) / 2 may fail at the following case : If, a = b = (2^31) – 1; i.e. INT_MAX. Now, (a+b) will cause overflow and hence formula (a + b) / 2 wont work Below is the implementation :  
C++
Java
Python3
C#
Javascript
// C++ code to compute average of two numbers#include <bits/stdc++.h>using namespace std; // Function to compute average of two numbersint compute_average(int a, int b){    return (a + b) / 2;} // Driver codeint main(){    // Assigning maximum integer value    int a = INT_MAX, b = INT_MAX;     // Average of two equal numbers is the same number    cout << "Actual average : " << INT_MAX << endl;     // Function to get the average of 2 numbers    cout << "Computed average : " << compute_average(a, b);     return 0;}
// Java code to compute average of two numbers import java.io.*; class GFG {     // Function to compute average of two numbersstatic int compute_average(int a, int b){    return (a + b) / 2;} // Driver code    public static void main (String[] args) {     // Assigning maximum integer value    int a = Integer.MAX_VALUE;    int b = Integer.MAX_VALUE;     // Average of two equal numbers is the same number    System.out.println("Actual average : " + Integer.MAX_VALUE);     // Function to get the average of 2 numbers    System.out.println("Computed average : " + compute_average(a, b));                      }// This code is contributed by ajit.   }
# Python 3 code to compute# average of two numbersimport sysfrom math import floor INT_MAX = 2147483647 # Function to compute# average of two numbersdef compute_average(a, b):    return floor((a + b) / 2) # Driver codeif __name__ == '__main__':         # Assigning maximum integer value    a = INT_MAX    b = -INT_MAX - 1     # Average of two equal numbers    # is the same number    print("Actual average : ", INT_MAX)     # Function to get the    # average of 2 numbers    print("Computed average : ",           compute_average(a, b)) # This code is contributed by# Surendra_Gangwar
// C#  code to compute average of two numbersusing System; public class GFG{         // Function to compute average of two numbersstatic int compute_average(int a, int b){    return (a + b) / 2;} // Driver code    static public void Main (){             // Assigning maximum integer value    int a =int.MaxValue;    int b = int.MaxValue;     // Average of two equal numbers is the same number    Console.WriteLine("Actual average : " + int.MaxValue);     // Function to get the average of 2 numbers    Console.WriteLine("Computed average : " + compute_average(a, b));    }//This code is contributed by akt_mit   }
<script>    // Javascript code to compute average of two numbers         const INT_MAX = 2147483647;         // Function to compute average of two numbers    function compute_average(a, b)    {        return Math.floor((a + b) / 2);    }         // Assigning maximum integer value    let a = INT_MAX;    let b = -INT_MAX-1;      // Average of two equal numbers is the same number    document.write("Actual average : " + INT_MAX + "</br>");      // Function to get the average of 2 numbers    document.write("Computed average : " + compute_average(a, b) + "</br>");     </script>
Output:  
Actual average : 2147483647
Computed average : -1
Improved Formula that does not cause overflow : Average = (a / 2) + (b / 2) + (((a % 2) + (b % 2)) / 2)Below is the implementation : 
C++
Java
Python3
C#
PHP
Javascript
// C++ code to compute average of two numbers#include <bits/stdc++.h>using namespace std; // Function to compute average of two numbersint compute_average(int a, int b){    return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);} // Driver codeint main(){    // Assigning maximum integer value    int a = INT_MAX, b = INT_MAX;     // Average of two equal numbers is the same number    cout << "Actual average : " << INT_MAX << endl;     // Function to get the average of 2 numbers    cout << "Computed average : " << compute_average(a, b);     return 0;}
// Java code to compute// average of two numbersimport java.io.*; class GFG{     // Function to compute// average of two numbersstatic int compute_average(int a,                           int b){    return (a / 2) + (b / 2) +           ((a % 2 + b % 2) / 2);} // Driver codepublic static void main (String[] args){ // Assigning maximum// integer valueint a = Integer.MAX_VALUE;int b = Integer.MAX_VALUE; // Average of two equal// numbers is the same numberSystem.out.println("Actual average : " +                     Integer.MAX_VALUE); // Function to get the// average of 2 numbersSystem.out.print("Computed average : ");System.out.println(compute_average(a, b));}} // This code is contributed by ajit
# Python code to compute# average of two numbersINT_MAX=2147483647 # Function to compute# average of two numbersdef compute_average(a,b):     return (a // 2) + (b // 2) + ((a % 2 + b % 2) // 2) # Driver codeif __name__ =="__main__":    # Assigning maximum integer value    a = INT_MAX    b = INT_MAX     # Average of two equal    # numbers is the same number    print( "Actual average : ",INT_MAX)     # Function to get the    # average of 2 numbers    print( "Computed average : ",            compute_average(a, b))      # This code is contributed# Shubham Singh(SHUBHAMSINGH10)
// C# code to compute// average of two numbers using System;  class GFG{      // Function to compute// average of two numbersstatic int compute_average(int a,                           int b){    return (a / 2) + (b / 2) +           ((a % 2 + b % 2) / 2);}  // Driver codepublic static void Main (){  // Assigning maximum// integer valueint a = int.MaxValue;int b = int.MaxValue;  // Average of two equal// numbers is the same numberConsole.Write("Actual average : " +                     int.MaxValue+"\n");  // Function to get the// average of 2 numbersConsole.Write("Computed average : ");Console.Write(compute_average(a, b));}}
<?php// PHP code to compute// average of two numbers// Function to compute// average of two numbersfunction compute_average($a,$b){    return ($a / 2) + ($b / 2) +        (($a % 2 + $b % 2) / 2);} // Driver code // Assigning maximum// integer value$a = 2147483648;$b = 2147483648; // Average of two equal// numbers is the same number$x = 2147483648;print("Actual average : ".$x); // Function to get the// average of 2 numbersprint("\nComputed average : ");print(compute_average($a, $b)); // This code is contributed by princiraj1992?>
<script> // javascript code to compute average of two numbersconst INT_MAX = 2147483647;  // Function to compute average of two numbersfunction compute_average( a,  b){    return parseInt(a / 2) + parseInt(b / 2) + ((a % 2 + b % 2) / 2);}  // Driver code     // Assigning maximum integer value    let a = INT_MAX, b = INT_MAX;     // Average of two equal numbers is the same number    document.write( "Actual average : " + INT_MAX +"<br/>");     // Function to get the average of 2 numbers   document.write( "Computed average : " + compute_average(a, b));      // This code is contributed by todaysgaurav </script>
Output:  
Actual average : 2147483647
Computed average : 2147483647
This article is contributed by Rohit Thapliyal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
ukasp
SHUBHAMSINGH10
SURENDRA_GANGWAR
princiraj1992
todaysgaurav
rameshtravel07
Nvidia
Mathematical
Nvidia
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Modulo Operator (%) in C/C++ with Examples
Print all possible combinations of r elements in a given array of size n
Operators in C / C++
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Find minimum number of coins that make a given value
Program to find sum of elements in a given array
Euclidean algorithms (Basic and Extended)
Program to print prime numbers from 1 to N.
Minimum number of jumps to reach end