+ 2
Need some help doing somethin in c++
Im trying to make a program to make expoent math but i cant do it for some reason, Here is the code int expoent(int b, int e){ long result = b; if (e > 1){ for (int i = e; i <= 1; i--){ result *= b; return result; } }else if (e = 1){ return result; } } int main() { int c; int f; cout << "input base" << endl; cin >> c; cout << "input expoent" << endl; cin >> f; int z = expoent (c,f); cout << "the result is " << z;
6 Answers
+ 1
Right after the closing bracket of for loop.
+ 2
thanks very much ir works now
+ 1
1. function expoent return type is int, but you're returning long.
2. in for loop, condition "i<=1", should be "i>=1".
3. return result, shouldn't be inside for loop otherwise it'll end the function in the first iteration only.
4. also takes care of power 0, result should be 1.
+ 1
in the loop condicionado since Im doing i -- the condicionado should be <= 1
+ 1
Imagine, user wants the result of, 2 raise to power 3.
Then in for loop, first value of i will be 3, which is greater than 1 and the condition will be false, so for loops will not be executed.
So the condition should be, "i>=1", to be exact it should be "i>1".
+ 1
oh ok
and the returno should be where?