# Fast average of two numbers without division

Given two numbers, find floor of their average without using division.

Input : x = 10, y = 12 Output : 11 Input : x = 10, y = 7 Output : 8 We take floor of sum.

The idea is to use right shift operator, instead of doing (x + y)/2, we do (x + y) >> 1

## C

`// C program to find average without using ` `// division. ` `#include <stdio.h> ` ` ` `int` `floorAvg(` `int` `x, ` `int` `y) { ` ` ` `return` `(x + y) >> 1; } ` ` ` `int` `main() { ` ` ` `int` `x = 10, y = 20; ` ` ` `printf` `(` `"\n\nAverage = %d\n\n"` `, floorAvg(x, y)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find average ` `// without using division ` `class` `GFG ` `{ ` ` ` `static` `int` `floorAvg(` `int` `x, ` `int` `y) { ` ` ` `return` `(x + y) >> ` `1` `; } ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `x = ` `10` `, y = ` `20` `; ` ` ` `System.out.print(floorAvg(x, y)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find average ` `# without using division. ` `def` `floorAvg(x, y): ` ` ` `return` `(x ` `+` `y) >> ` `1` ` ` `# Driver code ` `x ` `=` `10` `y ` `=` `20` ` ` `print` `(` `"Average "` `, floorAvg(x, y)) ` ` ` `# This code is contributed by sunny singh ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find average ` `// without using division ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `floorAvg(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `return` `(x + y) >> 1; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main (String[] args) ` ` ` `{ ` ` ` `int` `x = 10, y = 20; ` ` ` `Console.Write(` `"Average = "` `); ` ` ` `Console.Write(floorAvg(x, y)); ` ` ` `} ` `} ` ` ` `// This code is contributed by parashar... ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find average ` `// without using division. ` ` ` `// function returns ` `// the average ` `function` `floorAvg(` `$x` `, ` `$y` `) ` `{ ` ` ` `return` `(` `$x` `+ ` `$y` `) >> 1; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `$x` `= 10; ` ` ` `$y` `= 20; ` ` ` `echo` `"Average = "` `, floorAvg(` `$x` `, ` `$y` `); ` ` ` `// This Code is contributed by Ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Average = 15

**Applications :**

We need floor of average of two numbers in many standard algorithms like Merge Sort, Binary Search, etc. Since we use bitwise operator instead of division, the above way of finding average is faster.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- How to swap two numbers without using a temporary variable?
- Compute modulus division by a power-of-2-number
- Count total set bits in all numbers from 1 to n
- Add two numbers without using arithmetic operators
- Russian Peasant (Multiply two numbers using bitwise operators)
- Subtract two numbers without using arithmetic operators
- Calculate 7n/8 without using division and multiplication operators
- Cyclic Redundancy Check and Modulo-2 Division
- Print first n numbers with exactly two set bits
- Find Two Missing Numbers | Set 2 (XOR based solution)
- Check if a number can be expressed as a sum of consecutive numbers
- Multiplication of two numbers with shift operator
- Count smaller numbers whose XOR with n produces greater value
- Check if two numbers are equal without using arithmetic and comparison operators
- Numbers whose bitwise OR and sum with N are equal
- Count numbers whose sum with x is equal to XOR with x
- Closest (or Next) smaller and greater numbers with same number of set bits
- Check if two numbers are bit rotations of each other or not
- Find two numbers from their sum and XOR
- Check if binary representations of two numbers are anagram

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.