0
How do you write y^n in c++ im new to this programming language
2 Answers
0
Best is to use pow(y,n) function for calculating y^n
#include <iostream>
#include <cmath>
using namespace std;
int main (void){
long y, n;
cin>>y>>n;
long r = pow(y,n);
cout<<y<<" ^ "<<n<<" = "<<r;
}
OR
Create your own function like
long power(long y, long n){
long I, p=1;
for (i=1;i<=n;i++){
p = p * y;
}
return p;
}
OR recursive function
long power(long y, long n){
long I, p=1;
//base condition
if (n==1) return y;
return p * power(y, n-1);
}
- 1
you'll have tu use the math library (can't remember the name)