Program to calculate pow(x,n) using inbuilt power function
To solve the problem follow the below idea:
We can use inbuilt power function pow(x, n) to calculate xn
Below is the implementation of the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
int power(int x, int n)
{
// return type of pow()
// function is double
return (int)pow(x, n);
}
// Driver Code
int main()
{
int x = 2;
int n = 3;
// Function call
cout << (power(x, n));
}
// This code is contributed by hemantraj712.
// Java program for the above approach
import java.io.*;
class GFG {
public static int power(int x, int n)
{
// Math.pow() is a function that
// return floating number
return (int)Math.pow(x, n);
}
// Driver Code
public static void main(String[] args)
{
int x = 2;
int n = 3;
// Function call
System.out.println(power(x, n));
}
}
# Python3 program for the above approach
def power(x, n):
# Return type of pow()
# function is double
return pow(x, n)
# Driver Code
if __name__ == "__main__":
x = 2
n = 3
# Function call
print(power(x, n))
# This code is contributed by susmitakundugoaldanga
// C# program for the above approach
using System;
public class GFG {
public static int power(int x, int n)
{
// Math.pow() is a function that
// return floating number
return (int)Math.Pow(x, n);
}
// Driver code
static public void Main()
{
int x = 2;
int n = 3;
// Function call
Console.WriteLine(power(x, n));
}
}
<script>
// Javascript program for the above approach
function power( x, n)
{
// Math.pow() is a function that
// return floating number
return parseInt(Math.pow(x, n));
}
// Driver Code
let x = 2;
let n = 3;
document.write(power(x, n));
// This code is contributed by sravan kumar
</script>
Output
8
Time Complexity: O(log n)
Auxiliary Space: O(1), for recursive call stack
Write program to calculate pow(x, n)
Given two integers x and n, write a function to compute xn. We may assume that x and n are small and overflow doesn’t happen.
Examples :
Input : x = 2, n = 3
Output : 8Input : x = 7, n = 2
Output : 49
Naive Approach: To solve the problem follow the below idea:
A simple solution to calculate pow(x, n) would multiply x exactly n times. We can do that by using a simple for loop
Below is the implementation of the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Naive iterative solution to calculate pow(x, n)
long power(int x, unsigned n)
{
// Initialize result to 1
long long pow = 1;
// Multiply x for n times
for (int i = 0; i < n; i++) {
pow = pow * x;
}
return pow;
}
// Driver code
int main(void)
{
int x = 2;
unsigned n = 3;
// Function call
int result = power(x, n);
cout << result << endl;
return 0;
}
// Java program for the above approach
import java.io.*;
class Gfg {
// Naive iterative solution to calculate pow(x, n)
public static long power(int x, int n)
{
// Initialize result by 1
long pow = 1L;
// Multiply x for n times
for (int i = 0; i < n; i++) {
pow = pow * x;
}
return pow;
}
// Driver code
public static void main(String[] args)
{
int x = 2;
int n = 3;
System.out.println(power(x, n));
}
};
# Python3 program for the above approach
def power(x, n):
# initialize result by 1
pow = 1
# Multiply x for n times
for i in range(n):
pow = pow * x
return pow
# Driver code
if __name__ == '__main__':
x = 2
n = 3
# Function call
print(power(x, n))
// C# program for the above approach
using System;
public class Gfg {
// Naive iterative solution to calculate pow(x, n)
static long power(int x, int n)
{
// Initialize result by 1
long pow = 1L;
// Multiply x for n times
for (int i = 0; i < n; i++) {
pow = pow * x;
}
return pow;
}
// Driver code
public static void Main(String[] args)
{
int x = 2;
int n = 3;
Console.WriteLine(power(x, n));
}
};
// This code contributed by Pushpesh Raj
// Naive iterative solution to calculate pow(x, n)
function power( x, n)
{
// Initialize result to 1
let pow = 1;
// Multiply x for n times
for (let i = 0; i < n; i++) {
pow = pow * x;
}
return pow;
}
// Driver code
let x = 2;
let n = 3;
// Function call
let result = power(x, n);
console.log( result );
// This code is contributed by garg28harsh.
Output
8
Time Complexity: O(n)
Auxiliary Space: O(1)
pow(x, n) using recursion:
We can use the same approach as above but instead of an iterative loop, we can use recursion for the purpose.
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
int power(int x, int n)
{
// If x^0 return 1
if (n == 0)
return 1;
// If we need to find of 0^y
if (x == 0)
return 0;
// For all other cases
return x * power(x, n - 1);
}
// Driver Code
int main()
{
int x = 2;
int n = 3;
// Function call
cout << (power(x, n));
}
// This code is contributed by Aditya Kumar (adityakumar129)
// C program for the above approach
#include <stdio.h>
int power(int x, int n)
{
// If x^0 return 1
if (n == 0)
return 1;
// If we need to find of 0^y
if (x == 0)
return 0;
// For all other cases
return x * power(x, n - 1);
}
// Driver Code
int main()
{
int x = 2;
int n = 3;
// Function call
printf("%d\n", power(x, n));
}
// This code is contributed by Aditya Kumar (adityakumar129)
// Java program for the above approach
import java.io.*;
class GFG {
public static int power(int x, int n)
{
// If x^0 return 1
if (n == 0)
return 1;
// If we need to find of 0^y
if (x == 0)
return 0;
// For all other cases
return x * power(x, n - 1);
}
// Driver Code
public static void main(String[] args)
{
int x = 2;
int n = 3;
// Function call
System.out.println(power(x, n));
}
}
# Python3 program for the above approach
def power(x, n):
# If x^0 return 1
if (n == 0):
return 1
# If we need to find of 0^y
if (x == 0):
return 0
# For all other cases
return x * power(x, n - 1)
# Driver Code
if __name__ == "__main__":
x = 2
n = 3
# Function call
print(power(x, n))
# This code is contributed by shivani.
// C# program for the above approach
using System;
class GFG {
public static int power(int x, int n)
{
// If x^0 return 1
if (n == 0)
return 1;
// If we need to find of 0^y
if (x == 0)
return 0;
// For all other cases
return x * power(x, n - 1);
}
// Driver Code
public static void Main(String[] args)
{
int x = 2;
int n = 3;
// Function call
Console.WriteLine(power(x, n));
}
}
// This code is contributed by Rajput-Ji
<script>
// javascript program for the above approach
function power(x , n) {
// If x^0 return 1
if (n == 0)
return 1;
if (x == 0)
return 0;
// For all other cases
return x * power(x, n - 1);
}
// Driver Code
var x = 2;
var n = 3;
document.write(power(x, n));
// This code is contributed by Rajput-Ji
</script>
Output
8
Time Complexity: O(n)
Auxiliary Space: O(n) n is the size of the recursion stack