|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (8 bit)
Join Date: May 2006
Location: Illinois
Posts: 235
|
I'm having trouble with yet another Java problem for this class I'm taking in college. I'm supposed to add 1 + 1/2 + 1/3 +...+ 1/15, and print out the result using a for, do-while, or a while statement. Ill paste what I have right now.
double i, sum3; sum3 = 0; for (i = 1/15; i <= 1/15; i = i + (1/14)) { sum3 = sum3 + i; } System.out.println("The sum of 1 + (1/2) + (1/3) + (1/4) +...+ 1/15 = " + sum3); I know this is wrong, but I can't even get the system to print out anything to the command console. Also, I'm not sure if I'm using the right type of statement for this particular problem. If anyone knows how to do this, please help me. This one is driving me crazy!
__________________
ASUS M4A785TD-V EVO 785G | AMD Phenom II X4 955 | 2 x 2GB G.Skill Ripjaws DDR3 1600 | AMD Radeon HD 6970 | Lite-On DVD-RW | Western Digital Caviar Black 750GB | Antec Sonata III | Corsair AX850 | Windows 7 Home Premium 64-bit Biostar NF61S-M2 TE | AMD Athlon 64 X2 5000+ | 2 x 2GB OCZ Reaper DDR2 6400 | Radeon HD 4850 | Lite-On DVD-RW | Seagate Barracuda 120GB | Antec Sonata II | Antec EarthWatts 500W | Ubuntu 11.04 64-bit |
|
|
|
|
|
#2 |
|
Member (9 bit)
Join Date: Dec 2004
Posts: 289
|
You could do something like this:
Code:
public class asdf
{
public static void main(String [] args) {
double sum = 0;
for (int i = 1; i <= 15; i++) {
sum += (1/(double)(i));
}
System.out.println(sum);
}
}
__________________
Dell Inspiron 9300 Intel Pentium M 740 17" Wide Screen XGA+ Display 1GB PC-4200 Dual Channel DDR2 256MB NVIDA GeForce Go 6800 Apple MacBook (Black) Intel Core 2 Duo 2.16 Ghz 13" Wide screen Display 2GB Memory |
|
|
|
|
|
#3 |
|
Member (8 bit)
Join Date: May 2006
Location: Illinois
Posts: 235
|
Alright, that worked! Thanks a lot for the help.
|
|
|
|
|
|
#4 |
|
Member (10 bit)
Join Date: Jun 2000
Location: Dorset UK
Posts: 559
|
I don't know about anyone else but that went straight over my head!!! you clever people lol
Doug |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ITE controller?? driving me nuts... | erucader | Computer Hardware | 4 | 10-02-2005 04:09 AM |
| Touchpad driving me NUTS!! | Jaggannath | Computer Hardware | 19 | 05-31-2005 02:17 AM |
| Sata hd driving me nuts!!!!!!! | Clamptastic | Computer Hardware | 13 | 04-20-2005 08:00 AM |
| WinXP error on startup driving me nuts! | ReBeL | Windows Legacy Support (XP and earlier) | 1 | 01-23-2002 11:05 PM |
| Conect problem driving me nuts | Briab Guy | Windows Legacy Support (XP and earlier) | 8 | 09-05-2001 02:43 PM |