Write a program to compute the power of a given base number to a positive integer exponent. You may NOT use the Math.pow method. Input the base and exponent, make sure you check for valid input.
Input the base: 1.2
Input the exponent: 4
1.2 raised to 4 power is 2.0736
Here is what I have:
import javax.swing.JOptionPane;
public class program5
{
public static void main(String[] args)
{
double num1;
int num2;
String input;
input = JOptionPane.showInputDialog("Input the base");
num1 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Input the exponent");
num2 = Integer.parseInt(input);
do {
if (num1 < 0 || num2 < 0)
System.out.println("Those inputs are not valid");
} while (num1 < 0 || num2 < 0);
if (num1 > 0 && num2 > 0)
do {
}while(num1 < (num2-1));
double x = num1;
for(int i = 1; i < num2; i++)
x *= num2;
System.out.println(num1 + " raised to the " + num2 + " power is " + x );
}
}
I cant figure out the issue at the end of the program. If i type in 3.0 for the base, and 4 for the exponent, the output is 192.0. Also, no output is shown if i type in any number below 2.0 for the base. Same goes for other combinations like 2.0 and 5, 2.0 and 6, etc. Any suggestions on how to fix this?

New Topic/Question
Reply



MultiQuote




|