Java Examples

100+ tutorials, source code, and examples to help you develop creative and technical skills.

Java Program to find Factorial of a number using Recursion

Factorial.java

import java.util.Scanner;

/* Code From Learnoset - Learn Coding Online.
    Download Learnoset App from PlayStore to learn coding,
    projects, algorithms, error handling
 */
 
 // Java Program to find Factorial of a number using Recursion
public class Factorial {

    public static void main(String args[]) {

        // create Scanner class object to get input from user
        Scanner scanner = new Scanner(System.in);
		
        System.out.println("Enter a number:");
		
        //get user's enetered value from 'scanner' object into 'num' variable
        int num = scanner.nextInt();
		
        // called the function 'fact(int)'
        int factorial = fact(num);
		
        System.out.println("Factorial of "+num+" is = " + factorial);

    }
	
	// created static function fact(int)
    static int fact(int num) {
        
		int output;
        if (num == 1) {
            return 1;
        }
		
        // Recursion: Function calling itself!!
        output = fact(num - 1) * num;
        return output;
    }

}

Projects with Source Code + Video Tutorials

You can download our Java and Android Studio Projects with Source Code and Video Tutorials.

Complete-Chat-Application-Using-Firebase-Database
Complete Chat Application Using Firebase Database
Complete-Quotes-App-for-Android
Complete Quotes App for Android
How-to-create-Modern-Login-and-Register-screen-in-Android-Studio-|-Material-UI-Design-|-Part---06
How to create Modern Login and Register screen in Android Studio | Material UI Design | Part - 06

If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com

Follow us to learn Coding and get in touch with new Technologies.