I have to do a program that calculates the powers of the numbers entered by the user. The problem is that I have to do it using the while cycle and I really do not know what my condition must be inside the while. Any suggestions?
I have to do a program that calculates the powers of the numbers entered by the user. The problem is that I have to do it using the while cycle and I really do not know what my condition must be inside the while. Any suggestions?
You could solve the problem in the following way:
result = 1 - > This will be the value of the result, start with 1 and not with zero to not be zero when finding the power.
power =? - > would be the value of the exponent of the number you want to find the answer.
number =? - > value of the base number of the power
then in the while:
result = 1 power = 3 // example value number = 5 // example value start = 0 // to solve the while
while (start < power) {
// compare the start value with the power value to validate the while
result = result * number; // operates the power
start = start +1; // increase to 1 at the beginning so that the while has an end
}
After operating the while, the power would be in the variable "result"
You have to multiply while (while) the number of multiplications is less than or equal to the power that you entered
private int potencia;//cuanto lo queres elevar
private int total;//donde se va a guardar
private int var;//numero base
//aca te ingresan los valores
total = 1;
while(int cont <= potencia)
{
total = total * var;
cont ++;
}
//despues mostras total
The condition is that you put a counter on the counter sums in while and that is less than or equal to the power. Before entering the while you have to ask for the values of the base and power