Create a Java program that implements a recursive function to calculate the factorial of an integer input by the user.
main
6
720
import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); scanner.close(); System.out.println(factorial(num)); } public static int factorial(int num) { if (num == 0) { return 1; } else { return num * Factorial(num - 1); } } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Create a Java program that implements a recursive function to calculate the result of raising an integer to another integer. This function should be created using recursion.
Create a Java program that implements a recursive function called multiply() that takes two integer numbers requested from the user and returns the result of their multiplication.
Create a Java program that uses recursion to calculate a number in the Fibonacci series.
Create a Java program that implements a recursive function that receives a requested number from the user and returns the factorial of that number.
Create a Java program that requests a string from the user and implements a recursive function to reverse a string of characters.
Create a Java program that implements a recursive function to check if a string is a palindrome or not.
Practice with recursion exercises in Java. Learn to use recursive algorithms and check their efficiency.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.