Site icon Hitech Panda

You are given two integers: X and N. You have to calculate X raised to power N and print it.

You are given two integers: X and N. You have to calculate X raised to power N and print it.

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:

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:

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:

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.

Exit mobile version