Python code to calculate X raised to power N and print it.
# Input values for x and n x = 2 n = 3 # Calculate x raised to the power of n using the ** operator result = x ** n # Print the result print(result)
Explanation:
- The code starts by defining two variables
x
andn
and assigning them the values 2 and 3 respectively. - Next, it calculates
x raised to the power of n
by using the ** operator. - The ** operator is used to perform exponentiation, it raises the number to the left of the operator to the power of the number to the right of the operator.
- In this case, the variable
result
is assigned the value ofx
raised to the power ofn
. - Finally, it prints the result on the screen.
In this example, x=2
and n=3
so the output will be 8.
Alternatively, you can use the pow()
function to calculate the power of a number.
Output:
result = pow(x,n) print(result)
JAVA code to calculate X raised to power N and print it.
Here is an equivalent code in Java for calculating x raised to the power of n:
import java.lang.Math; // import the Math library for the pow() function public class Main { public static void main(String[] args) { // Input values for x and n int x = 2; int n = 3; // Calculate x raised to the power of n using the Math.pow() function double result = Math.pow(x, n); // Print the result System.out.println(result); } }
Explanation:
- The code starts by importing the Math library, which contains the
pow()
function. - Then it defines two variables
x
andn
and assigns them the values 2 and 3 respectively. - Next, it uses the
Math.pow()
function to calculatex
raised to the power ofn
. Thepow()
function takes two arguments: the base and the exponent. - The function returns a double value, so the result is assigned to a double variable
result
. - Finally, it prints the result on the screen using the
System.out.println()
function.
In this example, x=2
and n=3
so the output will be 8.0
Note that in Java, the pow()
function returns a double, so the result is stored in a double variable.
C++ code to calculate X raised to power N and print it.
Here is an equivalent code in C++ for calculating x raised to the power of n:
#include <iostream> #include <cmath> // include the cmath library for the pow() function int main() { // Input values for x and n int x = 2; int n = 3; // Calculate x raised to the power of n using the pow() function double result = pow(x, n); // Print the result std::cout << result << std::endl; return 0; }
Explanation:
- The code starts by including the
iostream
library for input/output operations and thecmath
library, which contains thepow()
function. - Then it defines two variables
x
andn
and assigns them the values 2 and 3 respectively. - Next, it uses the
pow()
function to calculatex
raised to the power ofn
. Thepow()
function takes two arguments: the base and the exponent. - The function returns a double value, so the result is assigned to a double variable
result
. - Finally, it prints the result on the screen using the
std::cout
function and thestd::endl
adds a new line after the result.
In this example, x=2
and n=3
so the output will be 8.
Note that in C++, the pow()
function also returns a double, so the result is stored in a double variable.