how to calculate 2^7 in cpp code

To calculate 2^7 in C++, you can use the power function from the <cmath> library. Here's the code snippet:

#include <iostream>
#include <cmath>

int main() {
    int base = 2;
    int exponent = 7;
    int result = pow(base, exponent);

    std::cout << "Result: " << result << std::endl;

    return 0;
}

Let's break down the code step by step:

  1. Include the necessary header files: cpp #include <iostream> #include <cmath>
  2. <iostream> is required to use the std::cout and std::endl for output.
  3. <cmath> is required to use the pow() function for calculating the power.

  4. Declare and initialize variables: cpp int base = 2; int exponent = 7;

  5. base variable stores the base value (2 in this case).
  6. exponent variable stores the exponent value (7 in this case).

  7. Calculate the power using the pow() function: cpp int result = pow(base, exponent);

  8. The pow() function takes two arguments: base and exponent.
  9. It calculates the power of base raised to the exponent and returns the result.
  10. The result is stored in the result variable.

  11. Print the result: cpp std::cout << "Result: " << result << std::endl;

  12. The std::cout is used to print the result to the console.
  13. The << operator is used to concatenate the string "Result: " with the value of result.
  14. The std::endl is used to insert a new line after printing the result.

  15. End the program: cpp return 0;

  16. This statement ends the program execution and returns the value 0 to indicate successful execution.

By running this code, you will get the output:

Result: 128

This means that 2^7 is equal to 128.