Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 X to the power N
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ProgrammingFun




PostPosted: Fri Oct 08, 2010 7:57 pm   Post subject: X to the power N

Hi, I have been trying to solve the following question:

Question:

Write a program that computes  where  is a floating point number and  is a positive integer. The program informs the user that  must be positive if the user enters a negative value. Of course,
XN = X * X * X * ... * X   
     --------------------
           N times


I wrote the following code but the program doesn't multiply properly.
Any ideas on what I may be doing wrong?
Thanks

Java:

   import java.util.*;

   public class Q6
   {
      public static void main (String[] args)
      {
     
         Scanner in = new Scanner (System.in);
         float x;
         int n;
         double x_n = 1.0;
     
         System.out.println ("Enter X");
         x = in.nextFloat();
     
         System.out.println ("Enter N");
         n = in.nextInt();
     
         if (n > 0){
         
            for (int c = 1; c <= n; c++);
            {
               x_n = x_n * x;
           
            }
                
            System.out.print ("\n" + x + " to the " + n + " power is: " + x_n);
         
         }
                
         //rest of program follows
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Fri Oct 08, 2010 8:09 pm   Post subject: RE:X to the power N

Is that what the question is, verbatim? It doesn't make any sense.

I gather the problem is to write a program that will calculate x to the power of n, where x is a floating point number, and x is a natural number?

Are you just getting unexpected output? The program looks fine to me, I'd suggest comparing what the program is giving you with what it should be doing, if it's a power of x times too big or small, the problem is probably the number of times the loop is running.
DanShadow




PostPosted: Fri Oct 08, 2010 9:16 pm   Post subject: RE:X to the power N

Your program logic looks fine.
My suggestion: use the Java function Sun made for this Wink

Math.pow(x,n);

Personally i'd swich your use of the float datatype to double though.
Quote:
x = in.nextFloat();
ProgrammingFun




PostPosted: Sat Oct 09, 2010 9:24 am   Post subject: RE:X to the power N

The problem with using the Math.pow function is that we are supposed to use only loops for the question. I still can't get the program to give proper output.

For example:

Enter X: 5
Enter Y: 5

5 to the power 5 is 5.0.
A.J




PostPosted: Sat Oct 09, 2010 9:55 am   Post subject: RE:X to the power N

Well, for starters, make sure you move your output statement to outside of the 'if(n>0)' statement, as if n = 0 your program won't output anything.
ProgrammingFun




PostPosted: Sat Oct 09, 2010 10:09 am   Post subject: RE:X to the power N

I created else ifs for n = 0 and n < 0.
chrisbrown




PostPosted: Sat Oct 09, 2010 10:43 am   Post subject: RE:X to the power N

Remove the semi-colon after your for loop.
ProgrammingFun




PostPosted: Sat Oct 09, 2010 1:46 pm   Post subject: Re: RE:X to the power N

chrisbrown @ Sat Oct 09, 2010 10:43 am wrote:
Remove the semi-colon after your for loop.

Thanks so much!!! That fixed the problem!!!
Sponsor
Sponsor
Sponsor
sponsor
copthesaint




PostPosted: Sat Oct 09, 2010 10:36 pm   Post subject: RE:X to the power N

*facepalm* I laughed so hard when I saw the semi colon after the for loop, I was going to just say something now, but I see chrisbrown beat me too it.
ProgrammingFun




PostPosted: Sun Oct 10, 2010 2:14 pm   Post subject: Re: RE:X to the power N

copthesaint @ Sat Oct 09, 2010 10:36 pm wrote:
*facepalm* I laughed so hard when I saw the semi colon after the for loop, I was going to just say something now, but I see chrisbrown beat me too it.

Embarassed
Thats the problem with me doing java....
I put semicolons where they aren't needed and don't put them where they are.
DtY




PostPosted: Mon Oct 11, 2010 1:57 pm   Post subject: RE:X to the power N

Haha, didn't see that semicolon.

Just remember, you have to have a complete statement after an if or a loop.

A complete statement is either a block of { to }, or something ending in a semicolon. When a semicolon directly follows an if or a loop, that is the statement attached to it (it's nothing).

The fun part comes where Java lets you put blocks of { to } without being attached to an if or loop.
INTELKING




PostPosted: Mon Oct 18, 2010 3:19 pm   Post subject: Re: X to the power N

Interesting...Thanks.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: