0
Calculate power (x^y) but not use power key.and show result (x^y). In C & C++ program
Please help me
11 odpowiedzi
+ 3
Hint: use for/while loop or recursion
+ 1
pratima sharma so you want to calculate x^y, but avoid using a predefined function, like pow()?
+ 1
If the answer to my question is yes, than as voja 🏊♂️ said try using for loop and continue multiplying the number x with itself until you got the y number of multiplications.
+ 1
pratima sharma
Show some code to prove you tried.
+ 1
pratima sharma Idk if you are , but it will most likely give ERROR on Turbo C++ , Turbo is old
0
int y = 5;
int x = 2;
int x2 = x;
for(int i = 0; i < y; i++){
x = x * x2;
}
cout << x << endl;
sorry i dont have the time to check if this is correct but i think it is, im in a hurry so sorry it is wrong :)
0
Sorry cat sauce your program in run time error
0
pratima sharma what what, how is that even possible
btw its c++
0
pratima sharma i check it out myself and it worked just fine, this is the code:
c++
#include <iostream>
using namespace std;
int main() {
int y = 5;
int x = 2;
int x2 = x;
for(int i = 0; i < y-1; i++){
x = x * x2;
}
cout << x << endl;
return 0;
}
0
Ok I checked
0
pratima sharma did it work?