Answer:
The program to this question can be given as follows:
Program:
//class
public class factorial //defining class
{
//method fact
public static long fact(int x1) //defining method fact
{
//conditional statement
if (x1 <= 1) //if block checks parameter value less then equal to 1
{
return 1; //return value
}
else //else part
{
return (fact(x1 - 1) * (long) x1); //return factors using recursive function
}
}
//method main
public static void main(String[] args) //defining main method
{
long data=fact(5);//defining variable that holds function value
System.out.println("Factorial is: "+data); //print value
}
}
Output:
Factorial is: 120
Explanation:
In the above java program, a class is "factorial" is defined, inside the class, a static method "fact" is declared, that accepts an integer parameter that is "x1" and returns a long value, inside this method a conditional statement is used.