xxxxxxxxxx
#include <cmath> //library for the function
pow(x,y);
Given two numbers base (x) and exponent (y), pow() function finds x raised to the power of y.
xxxxxxxxxx
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// computes 5 raised to the power 3
cout << pow(5, 3);
return 0;
}
// Output: 125
xxxxxxxxxx
/* pow example */
#include <stdio.h> /* printf */
#include <math.h> /* pow */
int main ()
{
printf ("7 ^ 3 = %f\n", pow (7.0, 3.0) );
printf ("4.73 ^ 12 = %f\n", pow (4.73, 12.0) );
printf ("32.01 ^ 1.54 = %f\n", pow (32.01, 1.54) );
return 0;
}
xxxxxxxxxx
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll power(ll a,ll b){
ll ans = 1;
while(b--){
ans *= a;
}
return ans;
}
int main(){
ll x,y;
cin >> x >> y;
ll x_power_y = power(x,y);
cout << x_power_y << endl;
}
xxxxxxxxxx
/* pow example */
#include <stdio.h> /* printf */
#include <math.h> /* pow */
int main ()
{
printf ("7 ^ 3 = %f\n", pow (7.0, 3.0) ); //7 ^ 3 = 343.000000
printf ("4.73 ^ 12 = %f\n", pow (4.73, 12.0) ); //4.73 ^ 12 = 125410439.217423
printf ("32.01 ^ 1.54 = %f\n", pow (32.01, 1.54) ); //32.01 ^ 1.54 = 208.036691
return 0;
}
xxxxxxxxxx
// CPP program to illustrate
// power function
#include <bits/stdc++.h>
using namespace std;
int main()
{
double x = 6.1, y = 4.8;
// Storing the answer in result.
double result = pow(x, y);
// printing the result upto 2
// decimal place
cout << fixed << setprecision(2) << result << endl;
return 0;
}
xxxxxxxxxx
//* exponent or power of x using loop
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 1, power = 0, temp = 0;
cin >> x >> power;
temp = x;
for (int i = 1; i < power; i++)
{
x *= temp;
}
cout << x << endl;
return 0;
}
c++ power of
xxxxxxxxxx
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// computes 5 raised to the power 3
cout << pow(5, 3);
return 0;
}
// Output: 125