Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 18th, 2012, 09:19 PM
Daler Daler is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 Daler User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 56 sec
Reputation Power: 0
Unhappy Homework - Please, help.

Hey guys,

I have a little problem here. The program runs and everything is fine except the actual result. The question is:

Design Java code to calculate and print ∑ (n! + 5) / (n+1)! ; n is an integer from 1 to 10.

I have written the code and I it's here:

Quote:
/**
*Question #2
*/

public class Sum
{
public static void main (String [] args)
{
long a = 1;
long b = 2 * a;
double sum = (a + 5.0) / b;

long n = 3;
while(n<=11)
{
sum = sum + (double)(a + 5.0) / b;
b = n * b;
a = a * (n - 1); //probably mistake here
n++;
}//end of a loop

System.out.println("The answer is " + sum);
}
}


I checked on my calculator and the result is 5.611286476

But my output is 8.520377259700176

Please, guys. I don't know where should I go. Help me find my mistake!

Reply With Quote
  #2  
Old November 18th, 2012, 11:10 PM
slink's Avatar
slink slink is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 73 slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 17 h 10 m 20 sec
Reputation Power: 14
Hi Daler,

The trick with programming is to keep things simple. Instead of trying to do everything in one go, try separating things out into functions. For example, you are using factorials, so break that out into a function:

Code:
public static long factorial(long x) {
    long factorial = 1;
    for (int i = 1; i <= x; i++) {
        factorial *= i;
    }
    return factorial;
}


Then you can use that in your main method.

It is generally better to write code that is easily understood (and debugged) than something that is perfectly optomized.

Hope this helps.
slink

Reply With Quote
  #3  
Old November 19th, 2012, 08:04 AM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,828 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 11 h 21 m 19 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
One thing I notice about your code is you're adding the first term twice.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Please, help.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap