By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

I need help geting my program to work properly?

P: 3
I don't know exactly whats wrong, my program works, but, I need it to be able to go all the up to "2 to the power of 295 is 6.36574E+88" but my program gives 31 a negitive answer "2 to the power of 31 is -2147483648" then it gives 0 for the rest of the answers "2 to the power of 32 is 0" ect. I have been working ont this program for 4 days. I've had several other problems but this is the only one that I cant figure out. Can you please help me?

Expand|Select|Wrap|Line Numbers
  1. // File Name: PowerOf2.java
  2. // Name of Creator: MITCHEL WRIGHT
  3. // Date Started: 12-04-2006
  4. // Date Finished: 12-04-2006
  5. // Purpose: to find "2 to the power of #=____"
  6. // Assignment: Create a program that will tell you
  7. // "2 to the power of 0 is 1"-"2 to the power of 295 is 6.36574E+88"
  8. //===============PowerOf2.java======================
  9. public class PowerOf2 
  10. {
  11. public static void main (String args[])
  12. { int intarray[]= new int [296];
  13. for (int I=0;I<=295; I++)
  14. intarray[i]=1;
  15. for (int p=0; p<I;p++)
  16. intarray[i]*=2; 
  17. }
  18. for (int i=0; i<1; i++)
  19. System.out.println("2 to the power of "+I+" is " + intarray[i]);
  20. }
  21. }
  22. }
  23.  
Dec 8 '06 #1
Share this Question
Share on Google+
2 Replies


P: 26
I don't know exactly whats wrong, my program works, but, I need it to be able to go all the up to "2 to the power of 295 is 6.36574E+88" but my program gives 31 a negitive answer "2 to the power of 31 is -2147483648" then it gives 0 for the rest of the answers "2 to the power of 32 is 0" ect. I have been working ont this program for 4 days. I've had several other problems but this is the only one that I cant figure out. Can you please help me?

Expand|Select|Wrap|Line Numbers
  1. // File Name: PowerOf2.java
  2. // Name of Creator: MITCHEL WRIGHT
  3. // Date Started: 12-04-2006
  4. // Date Finished: 12-04-2006
  5. // Purpose: to find "2 to the power of #=____"
  6. // Assignment: Create a program that will tell you
  7. // "2 to the power of 0 is 1"-"2 to the power of 295 is 6.36574E+88"
  8. //===============PowerOf2.java======================
  9. public class PowerOf2 
  10. {
  11. public static void main (String args[])
  12. { int intarray[]= new int [296];
  13. for (int I=0;I<=295; I++)
  14. intarray[i]=1;
  15. for (int p=0; p<I;p++)
  16. intarray[i]*=2; 
  17. }
  18. for (int i=0; i<1; i++)
  19. System.out.println("2 to the power of "+I+" is " + intarray[i]);
  20. }
  21. }
  22. }
  23.  

Hey man,

Easiest way of solving your problem is to use Math.pow

Expand|Select|Wrap|Line Numbers
  1.  
  2. // File Name: PowerOf2.java
  3. // Name of Creator: MITCHEL WRIGHT
  4. // Date Started: 12-04-2006
  5. // Date Finished: 12-04-2006
  6. // Purpose: to find "2 to the power of #=____"
  7. // Assignment: Create a program that will tell you
  8. // "2 to the power of 0 is 1"-"2 to the power of 295 is 6.36574E+88"
  9. //===============PowerOf2.java======================  
  10. public class PowerOf2 
  11. {
  12.     public static void main (String args[])
  13.     {
  14.         for (int i=0; i < 296; i++) {
  15.             System.out.println("2 to the power of "+i+" is " + getPower(i));
  16.         }
  17.     }
  18.  
  19.     public static double getPower(double i) {
  20.         return Math.pow(2,i);
  21.     }
  22.  
  23. }
  24.  
May I suggest reading up on this:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#pow(double,%20double)

mharrison
Dec 8 '06 #2

Expert 100+
P: 1,510
In Java the int data type is a 32-bit signed two's complement integer, i.e. minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive), therefore it won't represent a number of the magnitude 6.36574E+88. Change your program to use double, e.g.
Expand|Select|Wrap|Line Numbers
  1.  double intarray[]= new double [296];
  2.  
The double data type is a double-precision 64-bit IEEE 754 floating point number and will represent the numeric range you require.
for example, running your program using double the output is
2 to the power of 0 is 1.0
2 to the power of 1 is 2.0
2 to the power of 2 is 4.0
2 to the power of 3 is 8.0
......
2 to the power of 292 is 7.957171782556586E87
2 to the power of 293 is 1.5914343565113173E88
2 to the power of 294 is 3.1828687130226345E88
2 to the power of 295 is 6.365737426045269E88
Dec 8 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.