Results 1 to 4 of 4
- 03-16-2011, 03:44 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
summation in java using for loops
I am trying to make a summation calculator for the following equation:

Where i is the lower limit (in this case it will always be 0) and n is the upper limit (in this problem the user will be asked to enter an integer value of the upper limit).
Here is my code (so far):
import java.io.*;
public class summation
{
public static void main(String args[]) throws IOException
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
int ul;
System.out.print("Enter the upper limit: ");
ul = Integer.parseInt(keybd.readLine());
}
}
I don't understand how to use a for loop to calculate the sum.
After that I got stuck. I appreciate any help i can get thanks! :)Last edited by java157; 03-16-2011 at 03:48 PM.
- 03-16-2011, 03:52 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
-
You will need some math functions.
Math.pow returns the value of the first argument raised to the power of the second argument.
Java Code:double Math.pow(double a, double b)
To calculate the factorial make your own loop e.g.
Java Code:private int factorial(int number) { int counter = number; int answer = counter; while (counter > 1) { answer *= --counter; } return answer; }
And then make a for-loop over your entire equation with your limits put in e.g.
Java Code:double totalSum = 0; double sum = 0; for (i=0; i<=n; i++) { ... totalSum += sum; } return totalSum;
But beware whereever you add an integer to the sum, convert it to a double first, or cast it to a double.Last edited by ozzyman; 03-16-2011 at 04:57 PM.
- 03-17-2011, 08:33 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 49
- Rep Power
- 0
Similar Threads
-
Summation Problem
By a7x75 in forum New To JavaReplies: 8Last Post: 02-05-2010, 05:38 AM -
Fibonacci summation problem
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 12-01-2009, 06:28 PM -
(Help) Quotient summation with prime numbers
By SapphireSpark in forum New To JavaReplies: 27Last Post: 10-24-2008, 08:28 AM -
(Help) Fraction Summation and Exponents
By SapphireSpark in forum New To JavaReplies: 19Last Post: 10-09-2008, 04:01 AM -
Help me: loops in java
By silvia in forum New To JavaReplies: 3Last Post: 07-19-2007, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks