5 Replies - 520 Views - Last Post: 09 February 2012 - 02:06 AM Rate Topic: -----

Topic Sponsor:

#1 RyanSorkhpoosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-October 11

Need help with calculating the exponent.

Posted 08 February 2012 - 04:11 AM

I have most of the program completed but I think there is an issue at the very end which is why i am not getting the correct output. The problem is:
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?

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with calculating the exponent.

#2 Mylo  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 493
  • Joined: 11-October 11

Re: Need help with calculating the exponent.

Posted 08 February 2012 - 04:19 AM

It seems you have been multiplying by the exponent (3 * 4 * 4 * 4) = 192, where it should have been 3 * 3 * 3 * 3 = 81.
Was This Post Helpful? 1
  • +
  • -

#3 RyanSorkhpoosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-October 11

Re: Need help with calculating the exponent.

Posted 08 February 2012 - 07:12 PM

Could you expand a bit on what you are saying? I still cant really understand where my mistake is. :(
Was This Post Helpful? 0
  • +
  • -

#4 YasuoDancez  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 113
  • Joined: 30-September 09

Re: Need help with calculating the exponent.

Posted 08 February 2012 - 08:14 PM

You could also look into the recursive algorithm for doing this.
Time analysis can be better with big O of log n.
Was This Post Helpful? 0
  • +
  • -

#5 Mylo  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 493
  • Joined: 11-October 11

Re: Need help with calculating the exponent.

Posted 08 February 2012 - 11:34 PM

Sure ryan, to calculate 5^4, you would do 5 * 5 * 5 * 5, that is 5 multiplied by itself 4 times. What you have done is essentially put 5 * 4 * 4 * 4 with 4 being the exponent.

You have


// num1 is the base, num2 is the exponent, you don't multiply by the exponent, only the base multiplied by itself (exponen[th]?) times

input = JOptionPane.showInputDialog("Input the exponent");
     num2 = Integer.parseInt(input);

for(int i = 1; i < num2; i++)
     x *= num2; // here you are multiplying by the exponent, not the "base" so *= num1 is better
     System.out.println(num1 + " raised to the " + num2 + " power is " + x );
}



I'd also like to ask what your trying to do here:

		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));



This will cause an infinite loop if a number is below zero. Also you are checking if something is true while that something is true, so that is not really useful. Then you make an infinite loop if your base is more than one less than the exponent and if there above zero.

That seems to make no sense to me, but maybe you were trying something else and couldn't figure it out. So just ask if you need help :)
Was This Post Helpful? 0
  • +
  • -

#6 RyanSorkhpoosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-October 11

Re: Need help with calculating the exponent.

Posted 09 February 2012 - 02:06 AM

Oh i understand now. Cant believe i made that mistake. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1