473,508 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I need help geting my program to work properly?

3 New Member
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
2 1820
mharrison
26 New Member
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
horace1
1,510 Recognized Expert Top Contributor
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

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

Similar topics

3
1513
by: Matik | last post by:
Hello everyone, I was wondering. Is it possible, to recive an event from ActiveX into database? I was looking alredy with notification services, but I think that's the wrong way. Lets say,...
23
8909
by: da Vinci | last post by:
Greetings, Onwards with the school studying. Working on a program and need to delete a file from a known location on the hard drive but cannot get anything I do to work. I have tried to use...
4
2149
by: Eddie | last post by:
I will be happy if someone will help me with this: - how to get mainboard serial number - how to get bioos serial number, bios name - how to get hard drive serial number (GetVolumeInformation...
11
1862
by: abico | last post by:
Write a program that reads in a sequence of words and prints them in reverse order.Using STACK.
0
286
by: Greg | last post by:
My app makes a call to a remote server (about which I know relatively little) using Webclient.UploadData. This is done once every 51/2 mins and is called by code within a worker thread. 95% of the...
13
3215
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
4
1892
by: Lemune | last post by:
Hello everyone. I'm using vb 2005. I'm creating program that run as service on windows. And in my program I need to use timer, so I'm using timer object from component. I try my source code on...
12
2983
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
5
3604
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7391
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.