Find average without division

Here given code implementation process.

/*
    C program for
    Find average without division
*/
#include <stdio.h>

// Returns the minimum of given values
int min(int a, int b)
{
	if (a < b)
	{
		return a;
	}
	return b;
}
// Returns the maximum of given values
int max(int a, int b)
{
	if (a > b)
	{
		return a;
	}
	return b;
}
void findAverage(int a, int b)
{
	// Find min value
	int minValue = min(a, b);
	// Find difference between given number
	int difference = max(a, b) - minValue;
	// Half the difference and added to min value
	int average = minValue + (difference >> 1);
	printf("\n Given (%d %d)", a, b);
	// Display the calculate floor average
	printf("\n Average : %d", average);
}
int main()
{
	// Test
	findAverage(60, 30);
	findAverage(4, 2);
	findAverage(-20, -30);
	findAverage(20, -30);
	return 0;
}

Output

 Given (60 30)
 Average : 45
 Given (4 2)
 Average : 3
 Given (-20 -30)
 Average : -25
 Given (20 -30)
 Average : -5
/*
    Java Program for
	Find average without division
*/
public class CalculateAverage
{
	// Returns the minimum of given values
	public int min(int a, int b)
	{
		if (a < b)
		{
			return a;
		}
		return b;
	}
	// Returns the maximum of given values
	public int max(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	public void findAverage(int a, int b)
	{
		// Find min value
		int minValue = min(a, b);
		// Find difference between given number
		int difference = max(a, b) - minValue;
		// Half the difference and added to min value
		int average = minValue + (difference >> 1);
		System.out.print("\n Given (" + a + " " + b + ")");
		// Display the calculate floor average
		System.out.print("\n Average : " + average);
	}
	public static void main(String[] args)
	{
		CalculateAverage task = new CalculateAverage();
		// Test
		task.findAverage(60, 30);
		task.findAverage(4, 2);
		task.findAverage(-20, -30);
		task.findAverage(20, -30);
	}
}

Output

 Given (60 30)
 Average : 45
 Given (4 2)
 Average : 3
 Given (-20 -30)
 Average : -25
 Given (20 -30)
 Average : -5
// Include header file
#include <iostream>

using namespace std;
/*
    C++ Program for
	Find average without division
*/
class CalculateAverage
{
	public:
		// Returns the minimum of given values
		int min(int a, int b)
		{
			if (a < b)
			{
				return a;
			}
			return b;
		}
	// Returns the maximum of given values
	int max(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	void findAverage(int a, int b)
	{
		// Find min value
		int minValue = this->min(a, b);
		// Find difference between given number
		int difference = this->max(a, b) - minValue;
		// Half the difference and added to min value
		int average = minValue + (difference >> 1);
		cout << "\n Given (" << a << " " << b << ")";
		// Display the calculate floor average
		cout << "\n Average : " << average;
	}
};
int main()
{
	CalculateAverage *task = new CalculateAverage();
	// Test
	task->findAverage(60, 30);
	task->findAverage(4, 2);
	task->findAverage(-20, -30);
	task->findAverage(20, -30);
	return 0;
}

Output

 Given (60 30)
 Average : 45
 Given (4 2)
 Average : 3
 Given (-20 -30)
 Average : -25
 Given (20 -30)
 Average : -5
// Include namespace system
using System;
/*
    Csharp Program for
	Find average without division
*/
public class CalculateAverage
{
	// Returns the minimum of given values
	public int min(int a, int b)
	{
		if (a < b)
		{
			return a;
		}
		return b;
	}
	// Returns the maximum of given values
	public int max(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	public void findAverage(int a, int b)
	{
		// Find min value
		int minValue = this.min(a, b);
		// Find difference between given number
		int difference = this.max(a, b) - minValue;
		// Half the difference and added to min value
		int average = minValue + (difference >> 1);
		Console.Write("\n Given (" + a + " " + b + ")");
		// Display the calculate floor average
		Console.Write("\n Average : " + average);
	}
	public static void Main(String[] args)
	{
		CalculateAverage task = new CalculateAverage();
		// Test
		task.findAverage(60, 30);
		task.findAverage(4, 2);
		task.findAverage(-20, -30);
		task.findAverage(20, -30);
	}
}

Output

 Given (60 30)
 Average : 45
 Given (4 2)
 Average : 3
 Given (-20 -30)
 Average : -25
 Given (20 -30)
 Average : -5
package main
import "fmt"
/*
    Go Program for
	Find average without division
*/
type CalculateAverage struct {}
func getCalculateAverage() * CalculateAverage {
	var me *CalculateAverage = &CalculateAverage {}
	return me
}
// Returns the minimum of given values
func(this CalculateAverage) min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
// Returns the maximum of given values
func(this CalculateAverage) max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func(this CalculateAverage) findAverage(a, b int) {
	// Find min value
	var minValue int = this.min(a, b)
	// Find difference between given number
	var difference int = this.max(a, b) - minValue
	// Half the difference and added to min value
	var average int = minValue + (difference >> 1)
	fmt.Print("\n Given (", a, " ", b, ")")
	// Display the calculate floor average
	fmt.Print("\n Average : ", average)
}
func main() {
	var task * CalculateAverage = getCalculateAverage()
	// Test
	task.findAverage(60, 30)
	task.findAverage(4, 2)
	task.findAverage(-20, -30)
	task.findAverage(20, -30)
}

Output

 Given (60 30)
 Average : 45
 Given (4 2)
 Average : 3
 Given (-20 -30)
 Average : -25
 Given (20 -30)
 Average : -5
<?php
/*
    Php Program for
	Find average without division
*/
class CalculateAverage
{
	// Returns the minimum of given values
	public	function min($a, $b)
	{
		if ($a < $b)
		{
			return $a;
		}
		return $b;
	}
	// Returns the maximum of given values
	public	function max($a, $b)
	{
		if ($a > $b)
		{
			return $a;
		}
		return $b;
	}
	public	function findAverage($a, $b)
	{
		// Find min value
		$minValue = $this->min($a, $b);
		// Find difference between given number
		$difference = $this->max($a, $b) - $minValue;
		// Half the difference and added to min value
		$average = $minValue + ($difference >> 1);
		echo("\n Given (".$a.
			" ".$b.
			")");
		// Display the calculate floor average
		echo("\n Average : ".$average);
	}
}

function main()
{
	$task = new CalculateAverage();
	// Test
	$task->findAverage(60, 30);
	$task->findAverage(4, 2);
	$task->findAverage(-20, -30);
	$task->findAverage(20, -30);
}
main();

Output

 Given (60 30)
 Average : 45
 Given (4 2)
 Average : 3
 Given (-20 -30)
 Average : -25
 Given (20 -30)
 Average : -5
/*
    Node JS Program for
	Find average without division
*/
class CalculateAverage
{
	// Returns the minimum of given values
	min(a, b)
	{
		if (a < b)
		{
			return a;
		}
		return b;
	}
	// Returns the maximum of given values
	max(a, b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	findAverage(a, b)
	{
		// Find min value
		var minValue = this.min(a, b);
		// Find difference between given number
		var difference = this.max(a, b) - minValue;
		// Half the difference and added to min value
		var average = minValue + (difference >> 1);
		process.stdout.write("\n Given (" + a + " " + b + ")");
		// Display the calculate floor average
		process.stdout.write("\n Average : " + average);
	}
}

function main()
{
	var task = new CalculateAverage();
	// Test
	task.findAverage(60, 30);
	task.findAverage(4, 2);
	task.findAverage(-20, -30);
	task.findAverage(20, -30);
}
main();

Output

 Given (60 30)
 Average : 45
 Given (4 2)
 Average : 3
 Given (-20 -30)
 Average : -25
 Given (20 -30)
 Average : -5
#   Python 3 Program for
# 	Find average without division
class CalculateAverage :
	#  Returns the minimum of given values
	def min(self, a, b) :
		if (a < b) :
			return a
		
		return b
	
	#  Returns the maximum of given values
	def max(self, a, b) :
		if (a > b) :
			return a
		
		return b
	
	def findAverage(self, a, b) :
		#  Find min value
		minValue = self.min(a, b)
		#  Find difference between given number
		difference = self.max(a, b) - minValue
		#  Half the difference and added to min value
		average = minValue + (difference >> 1)
		print("\n Given (", a ," ", b ,")", end = "")
		#  Display the calculate floor average
		print("\n Average : ", average, end = "")
	

def main() :
	task = CalculateAverage()
	#  Test
	task.findAverage(60, 30)
	task.findAverage(4, 2)
	task.findAverage(-20, -30)
	task.findAverage(20, -30)

if __name__ == "__main__": main()

Output

 Given ( 60   30 )
 Average :  45
 Given ( 4   2 )
 Average :  3
 Given ( -20   -30 )
 Average :  -25
 Given ( 20   -30 )
 Average :  -5
#   Ruby Program for
# 	Find average without division
class CalculateAverage 
	#  Returns the minimum of given values
	def min(a, b) 
		if (a < b) 
			return a
		end

		return b
	end

	#  Returns the maximum of given values
	def max(a, b) 
		if (a > b) 
			return a
		end

		return b
	end

	def findAverage(a, b) 
		#  Find min value
		minValue = self.min(a, b)
		#  Find difference between given number
		difference = self.max(a, b) - minValue
		#  Half the difference and added to min value
		average = minValue + (difference >> 1)
		print("\n Given (", a ," ", b ,")")
		#  Display the calculate floor average
		print("\n Average : ", average)
	end

end

def main() 
	task = CalculateAverage.new()
	#  Test
	task.findAverage(60, 30)
	task.findAverage(4, 2)
	task.findAverage(-20, -30)
	task.findAverage(20, -30)
end

main()

Output

 Given (60 30)
 Average : 45
 Given (4 2)
 Average : 3
 Given (-20 -30)
 Average : -25
 Given (20 -30)
 Average : -5
/*
    Scala Program for
	Find average without division
*/
class CalculateAverage()
{
	// Returns the minimum of given values
	def min(a: Int, b: Int): Int = {
		if (a < b)
		{
			return a;
		}
		return b;
	}
	// Returns the maximum of given values
	def max(a: Int, b: Int): Int = {
		if (a > b)
		{
			return a;
		}
		return b;
	}
	def findAverage(a: Int, b: Int): Unit = {
		// Find min value
		var minValue: Int = min(a, b);
		// Find difference between given number
		var difference: Int = max(a, b) - minValue;
		// Half the difference and added to min value
		var average: Int = minValue + (difference >> 1);
		print("\n Given (" + a + " " + b + ")");
		// Display the calculate floor average
		print("\n Average : " + average);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CalculateAverage = new CalculateAverage();
		// Test
		task.findAverage(60, 30);
		task.findAverage(4, 2);
		task.findAverage(-20, -30);
		task.findAverage(20, -30);
	}
}

Output

 Given (60 30)
 Average : 45
 Given (4 2)
 Average : 3
 Given (-20 -30)
 Average : -25
 Given (20 -30)
 Average : -5
/*
    Swift 4 Program for
	Find average without division
*/
class CalculateAverage
{
	// Returns the minimum of given values
	func min(_ a: Int, _ b: Int) -> Int
	{
		if (a < b)
		{
			return a;
		}
		return b;
	}
	// Returns the maximum of given values
	func max(_ a: Int, _ b: Int) -> Int
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	func findAverage(_ a: Int, _ b: Int)
	{
		// Find min value
		let minValue: Int = self.min(a, b);
		// Find difference between given number
		let difference: Int = self.max(a, b) - minValue;
		// Half the difference and added to min value
		let average: Int = minValue + (difference >> 1);
		print("\n Given (", a ," ", b ,")", terminator: "");
		// Display the calculate floor average
		print("\n Average : ", average, terminator: "");
	}
}
func main()
{
	let task: CalculateAverage = CalculateAverage();
	// Test
	task.findAverage(60, 30);
	task.findAverage(4, 2);
	task.findAverage(-20, -30);
	task.findAverage(20, -30);
}
main();

Output

 Given ( 60   30 )
 Average :  45
 Given ( 4   2 )
 Average :  3
 Given ( -20   -30 )
 Average :  -25
 Given ( 20   -30 )
 Average :  -5
/*
    Kotlin Program for
	Find average without division
*/
class CalculateAverage
{
	// Returns the minimum of given values
	fun min(a: Int, b: Int): Int
	{
		if (a < b)
		{
			return a;
		}
		return b;
	}
	// Returns the maximum of given values
	fun max(a: Int, b: Int): Int
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	fun findAverage(a: Int, b: Int): Unit
	{
		// Find min value
		val minValue: Int = this.min(a, b);
		// Find difference between given number
		val difference: Int = this.max(a, b) - minValue;
		// Half the difference and added to min value
		val average: Int = minValue + (difference shr 1);
		print("\n Given (" + a + " " + b + ")");
		// Display the calculate floor average
		print("\n Average : " + average);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: CalculateAverage = CalculateAverage();
	// Test
	task.findAverage(60, 30);
	task.findAverage(4, 2);
	task.findAverage(-20, -30);
	task.findAverage(20, -30);
}

Output

 Given (60 30)
 Average : 45
 Given (4 2)
 Average : 3
 Given (-20 -30)
 Average : -25
 Given (20 -30)
 Average : -5


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved