Can someone please give me the code


#1

Write a Java class called Factorial. This class defines a method
called factorial() which takes as its only parameter an int called n, and returns an int representing the factorial of n. The factorial of an integer n, denoted n!, is defined as n (n 1) (n 2) . . . 1 (but note that
0! is 1). You MAY assume that n is a non-negative integer.


#2

Please post a link to this exercise. Thanks.

I'm not a big Java user, but from what little I know, this should come pretty close to your requirements. Once we have the exercise in front of us we can test and debug it.

public class Factorial {
    public int factorial(int n) {
        int f = 1;
        while (n > 0) {
            f *= n;
            n -= 1;
        }
        return f;
    }
        
    public static void main(String[] args) {

    }
}

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.