0

How do you write y^n in c++ im new to this programming language

15th Mar 2017, 5:40 AM
mikaelhans
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); }
15th Mar 2017, 6:47 AM
ą¤¦ą„‡ą¤µą„‡ą¤‚ą¤¦ą„ą¤° ą¤®ą¤¹ą¤¾ą¤œą¤Ø (Devender)
ą¤¦ą„‡ą¤µą„‡ą¤‚ą¤¦ą„ą¤° ą¤®ą¤¹ą¤¾ą¤œą¤Ø (Devender) - avatar
- 1
you'll have tu use the math library (can't remember the name)
15th Mar 2017, 6:30 AM
Edi Lipovac
Edi Lipovac - avatar