[Solved] Recursion Factorial with Java

Recursion Factorial: Clinton and Collin tries to play a game on words. If a word is given to them, they need to find as much words as possible using the characters in the given word. While playing, they taught to find how many number of combinations are possible. But, they find it difficult to get it. Can you write a recursion function driven program to determine the total number of combinations they can generate.

InputFormat:

Input consists of single integer corresponds to the number of characters in the given word.

Outputformat:

Output consists of single integer corresponds to the total numbers of combinations.

[Refer sample input and output for further formatting specifications]

Sample input and output:

[All text in bold corresponds to input and the rest to output]

Enter the number of characters in the given word :

5

The number of combinations are 120

Solution

import java.util.*;

public class Main {
    public static void main(String[] args) {
        int n, a;
        System.out.println("Enter the number of characters in the given word :");
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        a = fact(n);
        System.out.println("The number of combinations are " + a);
    }

    public static int fact(int n) {
        if (n == 0) {
            return 1;
        }
        return n * fact(n - 1);
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *