Method 1 (Using Nested Loops) We can calculate power by using repeated addition. For example to calculate 5^6. 1) First 5 times add 5, we get 25. (5^2) 2) Then 5 times add 25, we get 125. (5^3) 3) Then 5 times add 125, we get 625 (5^4) 4) Then 5 times add 625, we get 3125 (5^5) 5) Then 5 times add 3125, we get 15625 (5^6)
C++
// C++ code for power function
#include <bits/stdc++.h>
usingnamespacestd;
/* Works only if a >= 0 and b >= 0 */
intpow(inta, intb)
{
if(b == 0)
return1;
intanswer = a;
intincrement = a;
inti, j;
for(i = 1; i < b; i++)
{
for(j = 1; j < a; j++)
{
answer += increment;
}
increment = answer;
}
returnanswer;
}
// Driver Code
intmain()
{
cout << pow(5, 3);
return0;
}
// This code is contributed
// by rathbhupendra
C
#include<stdio.h>
/* Works only if a >= 0 and b >= 0 */
intpow(inta, intb)
{
//base case : anything raised to the power 0 is 1
if(b == 0)
return1;
intanswer = a;
intincrement = a;
inti, j;
for(i = 1; i < b; i++)
{
for(j = 1; j < a; j++)
{
answer += increment;
}
increment = answer;
}
returnanswer;
}
/* driver program to test above function */
intmain()
{
printf("\n %d", pow(5, 3));
getchar();
return0;
}
Java
importjava.io.*;
classGFG {
/* Works only if a >= 0 and b >= 0 */
staticintpow(inta, intb)
{
if(b == 0)
return1;
intanswer = a;
intincrement = a;
inti, j;
for(i = 1; i < b; i++) {
for(j = 1; j < a; j++) {
answer += increment;
}
increment = answer;
}
returnanswer;
}
// driver program to test above function
publicstaticvoidmain(String[] args)
{
System.out.println(pow(5, 3));
}
}
// This code is contributed by vt_m.
Python
# Python 3 code for power
# function
# Works only if a >= 0 and b >= 0
defpow(a,b):
if(b==0):
return1
answer=a
increment=a
fori inrange(1,b):
forj inrange(1,a):
answer+=increment
increment=answer
returnanswer
# driver code
print(pow(5,3))
# this code is contributed
# by Sam007
C#
usingSystem;
classGFG
{
/* Works only if a >= 0 and b >= 0 */
staticintpow(inta, intb)
{
if(b == 0)
return1;
intanswer = a;
intincrement = a;
inti, j;
for(i = 1; i < b; i++) {
for(j = 1; j < a; j++) {
answer += increment;
}
increment = answer;
}
returnanswer;
}
// driver program to test
// above function
publicstaticvoidMain()
{
Console.Write(pow(5, 3));
}
}
// This code is contributed by Sam007
PHP
<?php
// Works only if a >= 0
// and b >= 0
functionpoww($a, $b)
{
if($b== 0)
return1;
$answer= $a;
$increment= $a;
$i;
$j;
for($i= 1; $i< $b; $i++)
{
for($j= 1; $j< $a; $j++)
{
$answer+= $increment;
}
$increment= $answer;
}
return$answer;
}
// Driver Code
echo( poww(5, 3));
// This code is contributed by nitin mittal.
?>
Javascript
<script>
/* Works only if a >= 0 and b >= 0 */
functionpow(a , b)
{
if(b == 0)
return1;
varanswer = a;
varincrement = a;
vari, j;
for(i = 1; i < b; i++)
{
for(j = 1; j < a; j++)
{
answer += increment;
}
increment = answer;
}
returnanswer;
}
// driver program to test above function
document.write(pow(5, 3));
// This code is contributed by shikhasingrajput
</script>
Output :
125
Time Complexity: O(a * b)
Auxiliary Space:O(1)
Method 2 (Using Recursion) Recursively add a to get the multiplication of two numbers. And recursively multiply to get a raise to the power b.
C++
#include<bits/stdc++.h>
usingnamespacestd;
/* A recursive function to get x*y */
intmultiply(intx, inty)
{
if(y)
return(x + multiply(x, y - 1));
else
return0;
}
/* A recursive function to get a^b
Works only if a >= 0 and b >= 0 */
intpow(inta, intb)
{
if(b)
returnmultiply(a, pow(a, b - 1));
else
return1;
}
// Driver Code
intmain()
{
cout << pow(5, 3);
getchar();
return0;
}
// This code is contributed
// by Akanksha Rai
C
#include<stdio.h>
/* A recursive function to get a^b
Works only if a >= 0 and b >= 0 */
intpow(inta, intb)
{
if(b)
returnmultiply(a, pow(a, b-1));
else
return1;
}
/* A recursive function to get x*y */
intmultiply(intx, inty)
{
if(y)
return(x + multiply(x, y-1));
else
return0;
}
/* driver program to test above functions */
intmain()
{
printf("\n %d", pow(5, 3));
getchar();
return0;
}
Java
importjava.io.*;
classGFG {
/* A recursive function to get a^b
Works only if a >= 0 and b >= 0 */
staticintpow(inta, intb)
{
if(b > 0)
returnmultiply(a, pow(a, b - 1));
else
return1;
}
/* A recursive function to get x*y */
staticintmultiply(intx, inty)
{
if(y > 0)
return(x + multiply(x, y - 1));
else
return0;
}
/* driver program to test above functions */
publicstaticvoidmain(String[] args)
{
System.out.println(pow(5, 3));
}
}
// This code is contributed by vt_m.
Python3
defpow(a,b):
if(b):
returnmultiply(a, pow(a, b-1));
else:
return1;
# A recursive function to get x*y *
defmultiply(x, y):
if(y):
return(x +multiply(x, y-1));
else:
return0;
# driver program to test above functions *
print(pow(5, 3));
# This code is contributed
# by Sam007
C#
usingSystem;
classGFG
{
/* A recursive function to get a^b
Works only if a >= 0 and b >= 0 */
staticintpow(inta, intb)
{
if(b > 0)
returnmultiply(a, pow(a, b - 1));
else
return1;
}
/* A recursive function to get x*y */
staticintmultiply(intx, inty)
{
if(y > 0)
return(x + multiply(x, y - 1));
else
return0;
}
/* driver program to test above functions */
publicstaticvoidMain()
{
Console.Write(pow(5, 3));
}
}
// This code is contributed by Sam007
PHP
<?php
/* A recursive function to get a^b
Works only if a >= 0 and b >= 0 */
functionp_ow( $a, $b)
{
if($b)
returnmultiply($a,
p_ow($a, $b- 1));
else
return1;
}
/* A recursive function
to get x*y */
functionmultiply($x, $y)
{
if($y)
return($x+ multiply($x, $y- 1));
else
return0;
}
// Driver Code
echopow(5, 3);
// This code is contributed by anuj_67.
?>
Javascript
<script>
// A recursive function to get a^b
// Works only if a >= 0 and b >= 0
functionpow(a, b)
{
if(b > 0)
returnmultiply(a, pow(a, b - 1));
else
return1;
}
// A recursive function to get x*y
functionmultiply(x, y)
{
if(y > 0)
return(x + multiply(x, y - 1));
else
return0;
}
// Driver code
document.write(pow(5, 3));
// This code is contributed by gauravrajput1
</script>
Output :
125
Time Complexity: O(b)
Auxiliary Space: O(b)
Method 3 (Using bit masking)
approach
we can a^n (let’s say 3^5) as 3^4 * 3^0 * 3^1 = 3^, so we can represent 5 as its binary i.e. 101
t
C++
#include <iostream>
usingnamespacestd;
//function calculating power
longlongpow(inta, intn){
intans=1;
while(n>0){
// calculate last bit(right most) bit of n
intlast_bit = n&1;
//if last bit is 1 then multiply ans and a
if(last_bit){
ans = ans*a;
}
//make a equal to square of a as on every succeeding bit it got squared like a^0, a^1, a^2, a^4, a^8
a = a*a;
n = n >> 1;
}
returnans;
}
//driver code
intmain() {
cout<<pow(3,5);
return0;
}
C
#include <stdio.h>
// function calculating power
longlongpow_(inta, intn){
intans = 1;
while(n > 0)
{
// calculate last bit(right most) bit of n
intlast_bit = n&1;
// if last bit is 1 then multiply ans and a
if(last_bit){
ans = ans*a;
}
//make a equal to square of a as on every succeeding bit it got squared like a^0, a^1, a^2, a^4, a^8
a = a*a;
n = n >> 1;
}
returnans;
}
// driver code
intmain()
{
// pow is an inbuilt function so I have used pow_ as a function name
printf("%lld",pow_(3,5));
return0;
}
// This code is contributed by akashish_.
Java
// Java program for the above approach
importjava.io.*;
importjava.util.*;
classGFG
{
// function calculating power
staticintpow(inta, intn){
intans = 1;
while(n > 0)
{
// calculate last bit(right most) bit of n
intlast_bit = n&1;
//if last bit is 1 then multiply ans and a
if(last_bit != 0){
ans = ans*a;
}
//make a equal to square of a as on every succeeding bit it got squared like a^0, a^1, a^2, a^4, a^8
a = a*a;
n = n >> 1;
}
returnans;
}
// Driver Code
publicstaticvoidmain(String[] args)
{
System.out.print(pow(3,5));
}
}
// This code is contributed by code_hunt.
Python3
# function calculating power
defpow(a, n):
ans =1
while(n > 0):
# calculate last bit(right most) bit of n
last_bit =n&1
# if last bit is 1 then multiply ans and a
if(last_bit):
ans =ans*a
# make a equal to square of a as on
# every succeeding bit it got squared
# like a^0, a^1, a^2, a^4, a^8
a =a*a
n =n >> 1
returnans
# driver code
print(pow(3, 5))
# This code is contributed by shinjanpatra
C#
// C# code to implement the approach
usingSystem;
usingSystem.Numerics;
usingSystem.Collections.Generic;
publicclassGFG {
// function calculating power
staticintpow(inta, intn){
intans = 1;
while(n > 0)
{
// calculate last bit(right most) bit of n
intlast_bit = n&1;
//if last bit is 1 then multiply ans and a
if(last_bit != 0){
ans = ans*a;
}
//make a equal to square of a as on every succeeding bit it got squared like a^0, a^1, a^2, a^4, a^8
a = a*a;
n = n >> 1;
}
returnans;
}
// Driver Code
publicstaticvoidMain(string[] args)
{
Console.Write(pow(3,5));
}
}
// This code is contributed by sanjoy_62.
Javascript
<script>
// function calculating power
functionpow(a, n){
let ans = 1;
while(n > 0)
{
// calculate last bit(right most) bit of n
let last_bit = n&1;
// if last bit is 1 then multiply ans and a
if(last_bit)
{
ans = ans*a;
}
// make a equal to square of a as on
// every succeeding bit it got squared
// like a^0, a^1, a^2, a^4, a^8
a = a*a;
n = n >> 1;
}
returnans;
}
// driver code
document.write(pow(3,5),"</br>");
// This code is contributed by shinjanpatra
</script>
PHP
<?php
// function calculating power
functionp_ow($a, $n){
$ans= 1;
while($n> 0)
{
// calculate last bit(right most) bit of n
$last_bit= $n&1;
// if last bit is 1 then multiply ans and a
if($last_bit)
{
$ans= $ans*$a;
}
// make a equal to square of a as on
// every succeeding bit it got squared
// like a^0, a^1, a^2, a^4, a^8
$a= $a*$a;
$n= $n>> 1;
}
return$ans;
}
// driver code
echo(p_ow(5,3));
?>
Time Complexity: O(log n)
Auxiliary Space: O(1)
Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy