Question 0-3

Fill in run() so that, when executed, the method will compute the factorial of a number the user types. A user executing the method should see the following, for example. (Boldface indicates what the user types.)

Number: 4
24
Recall that n factorial is the product of the integers up to n; for example, the factorial of 4 is 1*2*3*4 = 24.
import csbsju.cs160.*;

public class FindFactorial {
    public static void run() {



    }
}

Solution

import csbsju.cs160.*;

public class FindFactorial {
    public static void run() {
        IO.print("Number: ");
        int num = IO.readInt();
        int fact = 1;
        while(num > 0) {
            fact *= num;
            num--;
        }
        IO.println(fact);
    }
}

Back to 8:00 Quiz 0