Quick actions

cmd+k|ctrl+k

Navigation

Languages

Week1Mock1MCQ17

Snippet info

Language

Java

Visibility

public

Author

masterhun

Created

2024-04-02T02:53:34.674963Z

Updated

2024-04-02T03:06:12.382972Z

public class Main {
    
    
    // I. 
    public static int f_I(int n) 
    {
        int factorial = 1;
        for (int i = n; i > 0; i--)
        {
            factorial *= n;
        }
        return factorial;
    }
    
    // II. 
    public static int f_II(int n)
    {
        int factorial = 1;
        int j = 1;
        while (j <= n)
        {
            factorial *= j;
            j++;
        }
        return factorial;
    }
    
    // III. 
    public static int f_III(int n)
    {
        if (n == 1)
            return n;
        return n * f_III(n - 1);
    }
    
    
    public static void main(String[] args) {
        
        // Harbor, Week 1, Mock 1, PR_PracticeTest_1.pdf
        
        // AP CS A 
        
        // MCQ 17 
        
        System.out.println(f_I(3));
        // Output: 27
        // 27 is not equal to 3! 
        // 3! is 6.
        
        System.out.println(f_II(3));
        // Output: 6
        // 3! is 6.
        
        System.out.println(f_III(3));
        // Output: 6
        // 3! is 6.

        
    }
}
INFO