To understand this example, you should have the knowledge of the following Python programming topics:
Python pow()
Python for
Loop
Python while Loop
Example 1: Calculate power of a number using a while loop
base = 3
exponent = 4
result = 1
while exponent != 0:
result *= base
exponent-=1
print("Answer = " + str(result))
Output
Answer = 81
In this program, base and exponent are assigned values 3 and 4 respectively.
Using the while loop, we keep on multiplying the result by base
until the exponent becomes zero.
In this case, we multiply result by base 4 times in total, so result = 1 * 3 * 3 * 3 * 3 = 81.
Example 2: Calculate power of a number using a for loop
base = 3
exponent = 4
result = 1
for exponent in range(exponent, 0, -1):
result *= base
print("Answer = " + str(result))
Output
Answer = 81
Here, instead of using a while loop, we've used a for loop.
After each iteration, the exponent is decremented by 1, and the result is multiplied by the base exponent number of times.
Both programs above do not work if you have a negative exponent. For that,
you need to use the pow() function in the Python library.
Example 3: Calculate the power of a number using pow() function
base = 3
exponent = -4
result = pow(base, exponent)
print("Answer = " + str(result))
Output
Answer = 0.012345679012345678
pow() accepts two arguments: base and exponent. In the above example, 3 raised to the power -4 is calculated using pow().
View Discussion
Improve Article
Save Article
Read
Discuss
View Discussion
Improve Article
Save Article
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++
#include <bits/stdc++.h>
usingnamespacestd;
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;
}
intmain()
{
cout << pow(5, 3);
return0;
}
C
#include<stdio.h>
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;
}
intmain()
{
printf("\n %d", pow(5, 3));
getchar();
return0;
}
Java
importjava.io.*;
classGFG {
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;
}
publicstaticvoidmain(String[] args)
{
System.out.println(pow(5, 3));
}
}
Python
defpow(a,b):
if(b==0):
return1
answer=a
increment=a
fori inrange(1,b):
forj inrange(1,a):
answer+=increment
increment=answer
returnanswer
print(pow(5,3))
C#
usingSystem;
classGFG
{
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;
}
publicstaticvoidMain()
{
Console.Write(pow(5, 3));
}
}
PHP
<?php
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;
}
echo( poww(5, 3));
?>
Javascript
<script>
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;
}
document.write(pow(5, 3));
</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;
intmultiply(intx, inty)
{
if(y)
return(x + multiply(x, y - 1));
else
return0;
}
intpow(inta, intb)
{
if(b)
returnmultiply(a, pow(a, b - 1));
else
return1;
}
intmain()
{
cout << pow(5, 3);
getchar();
return0;
}
C
#include<stdio.h>
intpow(inta, intb)
{
if(b)
returnmultiply(a, pow(a, b-1));
else
return1;
}
intmultiply(intx, inty)
{
if(y)
return(x + multiply(x, y-1));
else
return0;
}
intmain()
{
printf("\n %d", pow(5, 3));
getchar();
return0;
}
Java
importjava.io.*;
classGFG {
staticintpow(inta, intb)
{
if(b > 0)
returnmultiply(a, pow(a, b - 1));
else
return1;
}
staticintmultiply(intx, inty)
{
if(y > 0)
return(x + multiply(x, y - 1));
else
return0;
}
publicstaticvoidmain(String[] args)
{
System.out.println(pow(5, 3));
}
}
Python3
defpow(a,b):
if(b):
returnmultiply(a, pow(a, b-1));
else:
return1;
defmultiply(x, y):
if(y):
return(x +multiply(x, y-1));
else:
return0;
print(pow(5, 3));
C#
usingSystem;
classGFG
{
staticintpow(inta, intb)
{
if(b > 0)
returnmultiply(a, pow(a, b - 1));
else
return1;
}
staticintmultiply(intx, inty)
{
if(y > 0)
return(x + multiply(x, y - 1));
else
return0;
}
publicstaticvoidMain()
{
Console.Write(pow(5, 3));
}
}
PHP
<?php
functionp_ow( $a, $b)
{
if($b)
returnmultiply($a,
p_ow($a, $b- 1));
else
return1;
}
functionmultiply($x, $y)
{
if($y)
return($x+ multiply($x, $y- 1));
else
return0;
}
echopow(5, 3);
?>
Javascript
<script>
functionpow(a, b)
{
if(b > 0)
returnmultiply(a, pow(a, b - 1));
else
return1;
}
functionmultiply(x, y)
{
if(y > 0)
return(x + multiply(x, y - 1));
else
return0;
}
document.write(pow(5, 3));
</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^5, so we can represent 5 as its binary i.e. 101
C++
#include <iostream>
usingnamespacestd;
longlongpow(inta, intn){
intans=1;
while(n>0){
intlast_bit = n&1;
if(last_bit){
ans = ans*a;
}
a = a*a;
n = n >> 1;
}
returnans;
}
intmain() {
cout<<pow(3,5);
return0;
}
C
#include <stdio.h>
longlongpow_(inta, intn){
intans = 1;
while(n > 0)
{
intlast_bit = n&1;
if(last_bit){
ans = ans*a;
}
a = a*a;
n = n >> 1;
}
returnans;
}
intmain()
{
printf("%lld",pow_(3,5));
return0;
}
Java
importjava.io.*;
importjava.util.*;
classGFG
{
staticintpow(inta, intn){
intans = 1;
while(n > 0)
{
intlast_bit = n&1;
if(last_bit != 0){
ans = ans*a;
}
a = a*a;
n = n >> 1;
}
returnans;
}
publicstaticvoidmain(String[] args)
{
System.out.print(pow(3,5));
}
}
Python3
defpow(a, n):
ans =1
while(n > 0):
last_bit =n&1
if(last_bit):
ans =ans*a
a =a*a
n =n >> 1
returnans
print(pow(3, 5))
C#
usingSystem;
usingSystem.Numerics;
usingSystem.Collections.Generic;
publicclassGFG {
staticintpow(inta, intn){
intans = 1;
while(n > 0)
{
intlast_bit = n&1;
if(last_bit != 0){
ans = ans*a;
}
a = a*a;
n = n >> 1;
}
returnans;
}
publicstaticvoidMain(string[] args)
{
Console.Write(pow(3,5));
}
}
Javascript
<script>
functionpow(a, n){
let ans = 1;
while(n > 0)
{
let last_bit = n&1;
if(last_bit)
{
ans = ans*a;
}
a = a*a;
n = n >> 1;
}
returnans;
}
document.write(pow(3,5),"</br>");
</script>
PHP
<?php
functionp_ow($a, $n){
$ans= 1;
while($n> 0)
{
$last_bit= $n&1;
if($last_bit)
{
$ans= $ans*$a;
}
$a= $a*$a;
$n= $n>> 1;
}
return$ans;
}
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.
How do you calculate power without a POW?
int Pow ( int a , int b ) {.
Let a ^ b be the input. The base is a, while the exponent is b..
Start with a power of 1..
Using a loop, execute the following instructions b times..
power = power * a..
The power system has the final solution, a ^ b..
How do you write a power function in Python?
The syntax for Power function:.
pow(x, y[, z]).
The value of 3**4 is : 81..
How do you find the power of 2 in Python?
A simple method for this is to simply take the log of the number on base 2 and if you get an integer then number is power of 2..
Another solution is to keep dividing the number by two, i.e, do n = n/2 iteratively. ... .
All power of two numbers have only one bit set..
How do you find the power of a number in Python?
How to find the power of a number in Python.
import math. print(math. pow(4,2)) Run. Importing math module in Python..
def power(n,e): res=0. for i in range(e): res *= n. return res. print(pow(4,2)) Run. ... .
def power(n, e): if e == 0: return 1. elif e == 1: return n. else: return (n*power(n, e-1)).