Results 1 to 4 of 4
 03162011, 04:44 PM #1Member
 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; 03162011 at 04:48 PM.
 03162011, 04:52 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,878
 Blog Entries
 7
 Rep Power
 21

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 forloop 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; 03162011 at 05:57 PM.
 03172011, 09:33 AM #4Member
 Join Date
 Apr 2009
 Posts
 49
 Rep Power
 0
Similar Threads

Summation Problem
By a7x75 in forum New To JavaReplies: 8Last Post: 02052010, 06:38 AM 
Fibonacci summation problem
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 12012009, 07:28 PM 
(Help) Quotient summation with prime numbers
By SapphireSpark in forum New To JavaReplies: 27Last Post: 10242008, 08:28 AM 
(Help) Fraction Summation and Exponents
By SapphireSpark in forum New To JavaReplies: 19Last Post: 10092008, 04:01 AM 
Help me: loops in java
By silvia in forum New To JavaReplies: 3Last Post: 07192007, 06:47 PM
Bookmarks