# Compute average of two numbers without overflow

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++

 `// C++ code to compute average of two numbers ` `#include ` `using` `namespace` `std; ` ` `  `// Function to compute average of two numbers ` `int` `compute_average(``int` `a, ``int` `b) ` `{ ` `    ``return` `(a + b) / 2; ` `} ` ` `  `// Driver code ` `int` `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

 `// Java code to compute average of two numbers ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `// Function to compute average of two numbers ` `static` `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.     ` `} `

## Python3

 `# Python 3 code to compute  ` `# average of two numbers ` `import` `sys ` `from` `math ``import` `floor ` ` `  `INT_MAX ``=` `2147483647` ` `  `# Function to compute  ` `# average of two numbers ` `def` `compute_average(a, b): ` `    ``return` `floor((a ``+` `b) ``/` `2``) ` ` `  `# Driver code ` `if` `__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#

 `// C#  code to compute average of two numbers ` `using` `System; ` ` `  `public` `class` `GFG{ ` `         `  `// Function to compute average of two numbers ` `static` `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     ` `} `

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++

 `// C++ code to compute average of two numbers ` `#include ` `using` `namespace` `std; ` ` `  `// Function to compute average of two numbers ` `int` `compute_average(``int` `a, ``int` `b) ` `{ ` `    ``return` `(a / 2) + (b / 2) + ((a % 2 + b % 2) / 2); ` `} ` ` `  `// Driver code ` `int` `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

 `// Java code to compute  ` `// average of two numbers ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to compute  ` `// average of two numbers ` `static` `int` `compute_average(``int` `a,  ` `                           ``int` `b) ` `{ ` `    ``return` `(a / ``2``) + (b / ``2``) + ` `           ``((a % ``2` `+ b % ``2``) / ``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.print(``"Computed average : "``); ` `System.out.println(compute_average(a, b)); ` `} ` `} ` ` `  `// This code is contributed by ajit `

## Python3

 `# Python code to compute  ` `# average of two numbers  ` `INT_MAX``=``2147483647` ` `  `# Function to compute  ` `# average of two numbers  ` `def` `compute_average(a,b):  ` ` `  `    ``return` `(a ``/``/` `2``) ``+` `(b ``/``/` `2``) ``+` `((a ``%` `2` `+` `b ``%` `2``) ``/``/` `2``) ` ` `  `# Driver code  ` `if` `__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#

 `// C# code to compute  ` `// average of two numbers ` ` `  `using` `System; ` `  `  `class` `GFG ` `{ ` `      `  `// Function to compute  ` `// average of two numbers ` `static` `int` `compute_average(``int` `a,  ` `                           ``int` `b) ` `{ ` `    ``return` `(a / 2) + (b / 2) + ` `           ``((a % 2 + b % 2) / 2); ` `} ` `  `  `// Driver code ` `public` `static` `void` `Main ()  ` `{ ` `  `  `// Assigning maximum ` `// integer value ` `int` `a = ``int``.MaxValue; ` `int` `b = ``int``.MaxValue; ` `  `  `// Average of two equal  ` `// numbers is the same number ` `Console.Write(``"Actual average : "` `+  ` `                     ``int``.MaxValue+``"\n"``); ` `  `  `// Function to get the ` `// average of 2 numbers ` `Console.Write(``"Computed average : "``); ` `Console.Write(compute_average(a, b)); ` `} ` `} `

## PHP

 ` `

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

3

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