Results 1 to 4 of 4
  1. #1
    java157 is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by java157 View Post
    I am trying to make a summation calculator for the following equation:

    Are you sure that summation is correct? There is no dependency on i in the term to be summed so the entire thing can be reduced to (n+1)*T where T is that term.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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.

  4. #4
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    just let n--> inf and your answer becomes

    double sum = Math.sin(x); // :-)

    that or modify the answer I gave for your other question regarding sums of odd factorials

    David

Similar Threads

  1. Summation Problem
    By a7x75 in forum New To Java
    Replies: 8
    Last Post: 02-05-2010, 05:38 AM
  2. Fibonacci summation problem
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 12-01-2009, 06:28 PM
  3. (Help) Quotient summation with prime numbers
    By SapphireSpark in forum New To Java
    Replies: 27
    Last Post: 10-24-2008, 08:28 AM
  4. (Help) Fraction Summation and Exponents
    By SapphireSpark in forum New To Java
    Replies: 19
    Last Post: 10-09-2008, 04:01 AM
  5. Help me: loops in java
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •