Fast average of two numbers without division Last Updated : 14 Mar, 2024 Comments Improve Suggest changes Like Article Like Report Given two numbers, find floor of their average without using division. Input : x = 10, y = 12Output : 11Input : x = 10, y = 7Output : 8We 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 <bits/stdc++.h> using namespace std; int floorAvg(int x, int y) { return (x + y) >> 1; } int main() { int x = 10, y = 20; cout << "Average = " << floorAvg(x, y) <<endl; return 0; } // This code is contributed by famously. 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; } 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. 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... JavaScript <script> // Javascript program to find // average without using // division. function floorAvg(x, y) { return (x + y) >> 1; } // Driver code var x = 10, y = 20; document.write("Average = " + floorAvg(x, y)); // This code is contributed by noob2000 </script> 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 ?> 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 OutputAverage = 15 Time Complexity : O(1) Auxiliary Space: O(1) 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. Comment More infoAdvertise with us Next Article Fast average of two numbers without division K Kishor Mishra Follow Improve Article Tags : Misc C/C++ Puzzles Bit Magic DSA Practice Tags : Bit MagicMisc Similar Reads Addition of two numbers without carry You are given two positive numbers n and m. You have to find a simple addition of both numbers but with a given condition that there is not any carry system in this addition. That is no carry is added at higher MSBs.Examples : Input : m = 456, n = 854 Output : 200 Input : m = 456, n = 4 Output : 450 6 min read Find average of two numbers using bit operation Given two integers x and y, the task is to find the average of these numbers i.e. (x + y) / 2 using bit operations. Note that this method will give result as floor value of the calculated average.Examples: Input: x = 2, y = 4 Output: 3 (2 + 4) / 2 = 3Input: x = 10, y = 9 Output: 9 Approach: Average 3 min read Find two numbers with difference and division both same as N Given an integer N, the task is to find two numbers a and b such that a / b = N and a - b = N. Print "No" if no such numbers are possible.Examples: Input: N = 6 Output: a = 7.2b = 1.2Explanation:For the given two numbers a and b, a/b = 6 = N and a-b = 6 = N Input: N = 1 Output: NoExplanation:There a 4 min read Highest power of two that divides a given number Given a number n, find the highest power of 2 that divides n.Examples: Input : n = 48 Output : 16 Highest power of 2 that divides 48 is 16.Input : n = 5 Output : 1 Highest power of 2 that divides 5 is 1. A simple solution is to try all powers of 2 one by one starting from 1, then 2, then 4 and so on 5 min read Division without using '/' operator Given two numbers, divide one from other without using '/' operator. Examples : Input : num1 = 13, num2 = 2 Output : 6 Input : num1 = 14, num2 = -2 Output : -7 Input : num1 = -11, num2 = 3 Output : -3 Input : num1 = 10, num2 = 10 Output : 1 Input : num1 = -6, num2 = -2 Output : 3 Input : num1 = 0, n 6 min read Like