how to power in cpp code example

Example 1: pow c++

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
  double base, exponent, result;		
  base = 3.4;	
  exponent = 4.4;	
  result = pow(base, exponent);		
  cout << base << "^" << exponent << " = " << result;		
  return 0;
}

Example 2: powers of 2 in cpp

// If not using any extra libraries
// Powers of 2. If finding 2^n
int main() {
  int ans = 1 >> n;
}
// Suppose we want to find x ^ y
void power(int x, int y) {
  int ans = 1;
  for (int i = 0; i < y; i++) {
    ans *= x;
  }
  return ans;
}

Tags:

Cpp Example