Java Programming

Recursive Factorial

Java example for calculate factorial using recursive method

8/28/2021
0 views
factorial-recursion.javaJava
import java.io.*;
import java.util.Scanner;

class calculating {

	/* Recursive method */
	int factorial_cal(int no) {
		if(no == 1)
			return 1;
		else
			return(no * factorial_cal(no - 1));
	}
}
										
class factorial {
	public static void main(String args[ ]) {
		int f;

		/* Creating object of the Scanner */
		Scanner r = new Scanner(System.in);
		System.out.println("Program to calculate factorial");
		System.out.println("Enter a number:");

		/* Create object of 'calculating' class */
		calculating cal = new calculating();

		/* Received data from user using scanner object and pass to recursive method */
		f = cal.factorial_cal(Integer.parseInt(r.nextLine());
		System.out.println("Factorial: "+f);
	}
}




/* Output */
Program to calculate factorial
Enter a number:
5

Factorial: 120
Java programmingRecursive FactorialJava Language Tutorial

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com