• Register

I have a java problem that says,
write code to complete printfactorial()'s recursive case. sample output if input is 5:

I researched a lot on this and find a way to make this happen.


So, after researching a lot, and getting a lot of unknown errors I come to an end. This program is producing the actual output I was looking for. 

    import java.util.Scanner;

    public class Factorial

        public static void main(String[] args) 

            int x, mul;

            Scanner s = new Scanner(System.in);

            System.out.print("Enter an integer:");

            x = s.nextInt();

            Factorial obj = new Factorial();

            mul = obj.printFactorial(x);

            System.out.println("Factorial of "+x+"is:"+mul);

        int printFactorial(int a)

            if(a > 1)

                return(a * printFactorial(a - 1));

            return 1;

I am using the printFactorial() in my recursive program to bring out my expected output. At the beginning of the program, we import and define the Scanner class to get an integer by the user. After getting the number it throws the number to the printFactorial()  to calculate the factorial value of it. In the next step, we called the function in my main function and print it as well. So, if the user provides the number 5 this program should return:

Factorial of 5 is: 120

Thanks. I hope this program will help you all.

posted Mar 19 in java by (17,740 points)