Programming

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

2 Mins read

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 and n 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 of x raised to the power of n.
  • 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 and n and assigns them the values 2 and 3 respectively.
  • Next, it uses the Math.pow() function to calculate x raised to the power of n. The pow() 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 the cmath library, which contains the pow() function.
  • Then it defines two variables x and n and assigns them the values 2 and 3 respectively.
  • Next, it uses the pow() function to calculate x raised to the power of n. The pow() 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 the std::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.

147 posts

About author
Hitechpanda strives to keep you updated on all the new advancements about the day-to-day technological innovations making it simple for you to go for a perfect gadget that suits your needs through genuine reviews.
Articles
Related posts
NewsProgramming

Python vs Java for Data Structures and Algorithms: Which is the Best Choice?

2 Mins read
When it comes to data structures and algorithms (DSA), two of the most popular languages are Python and Java. Both languages have…
Something Techy Something Trendy

Best place to stay tuned with latest infotech updates and news

Subscribe Us Today

Leave a Reply

Your email address will not be published. Required fields are marked *