Click here to Skip to main content
15,887,485 members

Comments by Member NFOC (Top 6 by date)

Member NFOC 11-Aug-18 13:53pm View    
I think this one is suitable answer.
Member NFOC 29-Jul-18 11:45am View    
Thank you for your response it helped.
Member NFOC 29-Jul-18 1:37am View    
I'm thinking your code is also not under the conditions predefined. As your code is taking more than three variables(three variables should be like this two for exponents and base and one in for loop), by taking b inside the loop you are making a code similar to this:
for (int i = 0, result = 1; i < expo; i++ ) {
result *= base;
}
Member NFOC 28-Jul-18 15:22pm View    
Sir I'm thinking your code is also not under the conditions predefined. As your code is taking more than three variables(three variables should be like this two for exponents and base and one in for loop), by taking b inside the loop you are making a code similar to this:
for (int i = 0, result = 1; i < expo; i++ ) {
result *= base;
}
Member NFOC 28-Jul-18 14:50pm View    
Deleted
But i think that your code i.e.,
for (int i = exponent - 1, exponent = 1; i >= 0; i--)
{
exponent *= base;
}
also won't work for zero.
Also it will not work if we will print the result outside the loop. As inside the loop it is taking exponent variable as different variable inside the loop.
Updated code will look like this
int a;
int b;
std::cout << "Let's find the b power of a: " << std::endl;
std::cout << "Enter b: ";
std::cin >> b;
std::cout << "Enter a: ";
std::cin >> a;

if (b == 0) {
std::cout << "Result: " << 1 << std::endl;

}

for (int i = b-1 , b = 1; i >= 0; i--) {


b *= a;

if (i == 0) {
std::cout << "Result: " << b << std::endl;
}

}
Thanks again for coming for rescue and helping me make more optimised code.