Write a program to find the factorial of a number by recursion?
Enter a positive integer number only.
/**
*
* @author Mindclues
*/
package com.tech.mindclues;
import java.util.Scanner;
public class FactorialByRecursion {
public static void main(String args[]){
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the element to find the factorial : ");
n= sc.nextInt();
int factorial = factorialMethod(n);
System.out.println("Factorial :" +factorial);
}
static int factorialMethod(int a){
if(a==0){
return 1;
}else{
return a*factorialMethod(a-1);
}
}
}
Enter the element to find the factorial : 3 Factorial :6




post a comment