Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Sum of Factorials

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sum of Factorials

    Hi, I'm working on this code right now. The first method returns the factorial of an integer (factorial of 3 = 1x2x3). And the second returns the sum of the factorials between 2 integers. This is correct according to my professor. I'm just confused as to why in the 2nd methods it isn't "total = total + Factorial(i);" instead of what I have.

    thanks for the help.

    public class LoopProblems
    {
        public int Factorial (int n){ 
     
            int total = 1;
     
            for (int i = 1; i <= n ; i++ ) {
     
                total = total * i;
            }
            return total;
     
        }
     
        public int SumOfFactorials (int start, int end) {
     
            int total = 1;
     
            for (int i = start; i <= end ; i++ ) {
     
                total = total * Factorial(i);
            }
            return total;
     
        } 
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sum of Factorials

    What is the output from when the posted code is executed?
    x * 0 = 0
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sum of Factorials

    So I changed it to 1. Is the code correct now?

    Thanks!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sum of Factorials

    Is the code correct now?
    Hard to tell without testing it.
    What happens when you compile and execute it?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sum of Factorials

    I'm not quite sure how to do that yet. We're not quite there yet in class. I'm going to teach myself how to do that soon. We just kind of work it out logically. To me , it should be
    total = total + Factorial(i);
    Thanks!

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Sum of Factorials

    Please post your code in code or highlight tags.

  7. #7
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Sum of Factorials

    Yeah. According to me too it should be (total = total + Factorial(i) because "total = total * Factorial(i);" doesnt make any logical sense relating to your question.

Similar Threads

  1. Sum from a file
    By mikerousse in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 4th, 2014, 05:04 PM
  2. A few tips in the direction of a lottery game, factorials
    By hholco in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 8th, 2013, 03:34 AM
  3. sum of digits
    By snarayana.murthy86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2013, 02:40 PM
  4. Sum of intervals
    By cisneros778 in forum Java Theory & Questions
    Replies: 3
    Last Post: February 21st, 2012, 04:08 PM
  5. Sum of even and odd integers
    By spikezone2004 in forum Object Oriented Programming
    Replies: 10
    Last Post: February 8th, 2012, 06:03 PM