(c) Write a program for finding the factorial of n by using recursion( data structure and algorithm)
Answers
Answered by
2
Answer:
import java.util.Scanner;
class FactorialDemo{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number:");
//Stored the entered value in variable
int num = scanner.nextInt();
//Called the user defined function fact
int factorial = fact(num);
System.out.println("Factorial of entered number is: "+factorial);
}
static int fact(int n)
{
int output;
if(n==1){
return 1;
}
//Recursion: Function calling itself!!
output = fact(n-1)* n;
return output;
}
}
Answered by
0
data structures and algorithms is root ab
Similar questions