JAVA: Create a Factorial application that prompts the user for a number and then displays its factorial. The factorial of a number is the product of all the positive integers from 1 to the number. For example, 5! = 5*4*3*2*1.
//Java program for calculating the factorial using recursion
import java.util.*;
public class factorial {
public static int func (int n) //function for calculating factorial
{
if(n==0||n==1) //base condition of recursion since 0!=1!=1
return 1;
else
return n*func(n-1); // this is recursion condition which says that multiplication will continue till n reaches base condition
}
public static void main(String[] args)
{
int x; //variable for accepting the number whose factorial is to be calculated
Scanner sc=new Scanner(System.in); //object of scanner class
System.out.println("Enter the number whose factorial is to be calculated:");
x=sc.nextInt(); //accepting the number whose factorial is to be calculated
System.out.println(x+"! = "+func(x)); //printing the result
}
}
JAVA: Create a Factorial application that prompts the user for a number and then displays its...