Skip to main content

Calculate power without using division and multiplication

The problem is to calculate the value of a number raised to a given power, but without using the multiplication or division operators. This means that we cannot simply use the * or / operators to multiply or divide the base number by itself the required number of times.

Instead, we need to use a different approach that only uses addition and other basic operators to perform the necessary calculations.

One way to do this is to use a nested loop structure to perform repeated additions of the base number. The outer loop iterates over the exponent value, while the inner loop iterates over the base value. Inside the inner loop, we add the result to itself for each iteration of the loop. This effectively multiplies the result by the base value. as follows:

// C program for 
// Calculate power without using division and multiplication
#include <stdio.h>

// This is calculating the power of given positive integers
void power(int base, int pow)
{
	// Define some resultant variable
	int result = base;
	int update = base;
	// Execute loop through by given power
	for (int i = 1; i < pow; ++i)
	{
		//  Execute loop through by given base number
		for (int j = 1; j < base; ++j)
		{
			// Calculate power
			result = result + update;
		}
		// Change
		update = result;
	}
	// Display calculated result
	printf(" (%d ^ %d) = %d\n", base, pow, result);
}
int main(int argc, char
	const *argv[])
{
	// Test Case
	power(2, 6);
	power(4, 5);
	power(1, 5);
	return 0;
}

input

 (2 ^ 6) = 64
 (4 ^ 5) = 1024
 (1 ^ 5) = 1
/*
  Java Program for
  Calculate power without using division and multiplication
*/
public class PowerOfNumber
{
	// This is calculating the power of given positive integers
	public void power(int base, int pow)
	{
		// Define some resultant variable
		int result = base;
		int update = base;
		// Execute loop through by given power
		for (int i = 1; i < pow; ++i)
		{
			//  Execute loop through by given base number
			for (int j = 1; j < base; ++j)
			{
				// Calculate power
				result = result + update;
			}
			// Change
			update = result;
		}
		// Display calculated result
		System.out.println(" (" + base + " ^ " + pow + ") = " + result);
	}
	public static void main(String[] args)
	{
		PowerOfNumber task = new PowerOfNumber();
		// Test Case
		task.power(2, 6);
		task.power(4, 5);
		task.power(1, 5);
	}
}

input

 (2 ^ 6) = 64
 (4 ^ 5) = 1024
 (1 ^ 5) = 1
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Calculate power without using division and multiplication
*/
class PowerOfNumber
{
	public:
		// This is calculating the power of given positive integers
		void power(int base, int pow)
		{
			// Define some resultant variable
			int result = base;
			int update = base;
			// Execute loop through by given power
			for (int i = 1; i < pow; ++i)
			{
				//  Execute loop through by given base number
				for (int j = 1; j < base; ++j)
				{
					// Calculate power
					result = result + update;
				}
				// Change
				update = result;
			}
			// Display calculated result
			cout << " (" << base << " ^ " << pow << ") = " << result << endl;
		}
};
int main()
{
	PowerOfNumber *task = new PowerOfNumber();
	// Test Case
	task->power(2, 6);
	task->power(4, 5);
	task->power(1, 5);
	return 0;
}

input

 (2 ^ 6) = 64
 (4 ^ 5) = 1024
 (1 ^ 5) = 1
// Include namespace system
using System;
/*
  Csharp Program for
  Calculate power without using division and multiplication
*/
public class PowerOfNumber
{
	// This is calculating the power of given positive integers
	public void power(int baseValue, int pow)
	{
		// Define some resultant variable
		int result = baseValue;
		int update = baseValue;
		// Execute loop through by given power
		for (int i = 1; i < pow; ++i)
		{
			//  Execute loop through by given base number
			for (int j = 1; j < baseValue; ++j)
			{
				// Calculate power
				result = result + update;
			}
			// Change
			update = result;
		}
		// Display calculated result
		Console.WriteLine(" (" + baseValue + " ^ " + pow + ") = " + result);
	}
	public static void Main(String[] args)
	{
		PowerOfNumber task = new PowerOfNumber();
		// Test Case
		task.power(2, 6);
		task.power(4, 5);
		task.power(1, 5);
	}
}

input

 (2 ^ 6) = 64
 (4 ^ 5) = 1024
 (1 ^ 5) = 1
<?php
/*
  Php Program for
  Calculate power without using division and multiplication
*/
class PowerOfNumber
{
	// This is calculating the power of given positive integers
	public	function power($base, $pow)
	{
		// Define some resultant variable
		$result = $base;
		$update = $base;
		// Execute loop through by given power
		for ($i = 1; $i < $pow; ++$i)
		{
			//  Execute loop through by given base number
			for ($j = 1; $j < $base; ++$j)
			{
				// Calculate power
				$result = $result + $update;
			}
			// Change
			$update = $result;
		}
		// Display calculated result
		echo " (".$base.
		" ^ ".$pow.
		") = ".$result.
		"\n";
	}
}

function main()
{
	$task = new PowerOfNumber();
	// Test Case
	$task->power(2, 6);
	$task->power(4, 5);
	$task->power(1, 5);
}
main();

input

 (2 ^ 6) = 64
 (4 ^ 5) = 1024
 (1 ^ 5) = 1
/*
  Node JS Program for
  Calculate power without using division and multiplication
*/
class PowerOfNumber
{
	// This is calculating the power of given positive integers
	power(base, pow)
	{
		// Define some resultant variable
		var result = base;
		var update = base;
		// Execute loop through by given power
		for (var i = 1; i < pow; ++i)
		{
			//  Execute loop through by given base number
			for (var j = 1; j < base; ++j)
			{
				// Calculate power
				result = result + update;
			}
			// Change
			update = result;
		}
		// Display calculated result
		console.log(" (" + base + " ^ " + pow + ") = " + result);
	}
}

function main()
{
	var task = new PowerOfNumber();
	// Test Case
	task.power(2, 6);
	task.power(4, 5);
	task.power(1, 5);
}
main();

input

 (2 ^ 6) = 64
 (4 ^ 5) = 1024
 (1 ^ 5) = 1
#  Python 3 Program for
#  Calculate power without using division and multiplication
class PowerOfNumber :
	#  This is calculating the power of given positive integers
	def power(self, base, pow) :
		result = base
		update = base
		#  Execute loop through by given power
		i = 1
		while (i < pow) :
			#   Execute loop through by given base number
			j = 1
			while (j < base) :
				#  Calculate power
				result = result + update
				j += 1
			
			#  Change
			update = result
			i += 1
		
		#  Display calculated result
		print(" (", base ," ^ ", pow ,") = ", result)
	

def main() :
	task = PowerOfNumber()
	#  Test Case
	task.power(2, 6)
	task.power(4, 5)
	task.power(1, 5)

if __name__ == "__main__": main()

input

 ( 2  ^  6 ) =  64
 ( 4  ^  5 ) =  1024
 ( 1  ^  5 ) =  1
#  Ruby Program for
#  Calculate power without using division and multiplication
class PowerOfNumber 
	#  This is calculating the power of given positive integers
	def power(base, pow) 
		#  Define some resultant variable
		result = base
		update = base
		#  Execute loop through by given power
		i = 1
		while (i < pow) 
			#   Execute loop through by given base number
			j = 1
			while (j < base) 
				#  Calculate power
				result = result + update
				j += 1
			end

			#  Change
			update = result
			i += 1
		end

		#  Display calculated result
		print(" (", base ," ^ ", pow ,") = ", result, "\n")
	end

end

def main() 
	task = PowerOfNumber.new()
	#  Test Case
	task.power(2, 6)
	task.power(4, 5)
	task.power(1, 5)
end

main()

input

 (2 ^ 6) = 64
 (4 ^ 5) = 1024
 (1 ^ 5) = 1
/*
  Scala Program for
  Calculate power without using division and multiplication
*/
class PowerOfNumber()
{
	// This is calculating the power of given positive integers
	def power(base: Int, pow: Int): Unit = {
		// Define some resultant variable
		var result: Int = base;
		var update: Int = base;
		// Execute loop through by given power
		var i: Int = 1;
		while (i < pow)
		{
			//  Execute loop through by given base number
			var j: Int = 1;
			while (j < base)
			{
				// Calculate power
				result = result + update;
				j += 1;
			}
			// Change
			update = result;
			i += 1;
		}
		// Display calculated result
		println(" (" + base + " ^ " + pow + ") = " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: PowerOfNumber = new PowerOfNumber();
		// Test Case
		task.power(2, 6);
		task.power(4, 5);
		task.power(1, 5);
	}
}

input

 (2 ^ 6) = 64
 (4 ^ 5) = 1024
 (1 ^ 5) = 1
/*
  Swift 4 Program for
  Calculate power without using division and multiplication
*/
class PowerOfNumber
{
	// This is calculating the power of given positive integers
	func power(_ base: Int, _ pow: Int)
	{
		// Define some resultant variable
		var result: Int = base;
		var update: Int = base;
		// Execute loop through by given power
		var i: Int = 1;
		while (i < pow)
		{
			//  Execute loop through by given base number
			var j: Int = 1;
			while (j < base)
			{
				// Calculate power
				result = result + update;
				j += 1;
			}
			// Change
			update = result;
			i += 1;
		}
		// Display calculated result
		print(" (", base ," ^ ", pow ,") = ", result);
	}
}
func main()
{
	let task: PowerOfNumber = PowerOfNumber();
	// Test Case
	task.power(2, 6);
	task.power(4, 5);
	task.power(1, 5);
}
main();

input

 ( 2  ^  6 ) =  64
 ( 4  ^  5 ) =  1024
 ( 1  ^  5 ) =  1
/*
  Kotlin Program for
  Calculate power without using division and multiplication
*/
class PowerOfNumber
{
	// This is calculating the power of given positive integers
	fun power(base: Int, pow: Int): Unit
	{
		// Define some resultant variable
		var result: Int = base;
		var update: Int = base;
		var i: Int = 1;
		while (i < pow)
		{
			var j: Int = 1;
			while (j < base)
			{
				// Calculate power
				result = result + update;
				j += 1;
			}
			// Change
			update = result;
			i += 1;
		}
		// Display calculated result
		println(" (" + base + " ^ " + pow + ") = " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: PowerOfNumber = PowerOfNumber();
	// Test Case
	task.power(2, 6);
	task.power(4, 5);
	task.power(1, 5);
}

input

 (2 ^ 6) = 64
 (4 ^ 5) = 1024
 (1 ^ 5) = 1

Note that this approach can be slow for large exponent values, as the inner loop will execute a large number of times. There are more efficient algorithms for calculating powers without using multiplication or division, such as the binary exponentiation algorithm, which can be implemented using only addition and bit shifting.





Comment

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