Question:

Can anyone help me with this Java loop problem?

by  |  earlier

0 LIKES UnLike

How do you write a program that will compute and display the sum of the factorials of the numbers from 1 to n, where n is a non-negative integer given by the user?

Example:

Input: n = 3; 1! 2! 3! = 9

Input: n = 4; 1! 2! 3! 4! = 33

 Tags:

   Report

2 ANSWERS


  1. Here is an example of a loop. This example is a sum of numbers but you should be able to see what to do.  You only need one loop though.

    Also given the wiki link I think that your predicted results are wrong.  Or this is not a factorial problem.


  2. function factorial(x)

    {

        return ( x > 1 ? x * factorial(x - 1) : 1);

    }

    function FactorialSum(x)

    {

        var sum = 0;

        for (var i = 0; i < x; i++)

        {

            sum += factorial(i + 1);

        }

        alert(sum);

    }

    Call FactorialSum() with whatever number the user entered.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.