KnowledgeBoat Logo

Computer Applications

Write a method fact(int n) to find the factorial of a number n. Include a main class to find the value of S where:
S = n! / (m!(n - m)!)
where, n! = 1 x 2 x 3 x ………. x n

Java

User Defined Methods

82 Likes

Answer

import java.util.Scanner;

public class KboatFactorial
{
    public long fact(int n) {
        
        long f = 1;
        
        for (int i = 1; i <= n; i++) {
            f *= i;
        }
        
        return f;
        
    }
    
    public static void main(String args[]) {
        
        KboatFactorial obj = new KboatFactorial();
        
        Scanner in = new Scanner(System.in);
        System.out.print("Enter m: ");
        int m = in.nextInt();
        System.out.print("Enter n: ");
        int n = in.nextInt();
        
        double s = (double)(obj.fact(n)) / (obj.fact(m) * obj.fact(n - m));
        System.out.println("S=" + s);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a function fact(int n) to find the factorial of a number n. Include a main class to find the value of S where: S = n! / (m!(n - m)!) where, n! = 1 x 2 x 3 x ………. x n

Answered By

30 Likes


Related Questions