Question
Java program

5. Write a short Java program to compute factorial of a given integer from input using recursive function.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for you:

//Program to calculate the factorial of a given number.
import java.io.*;
import java.util.*;
class FactorialRecursive
{
    public static void main(String[] args)
    {
       //Reads a value from the console.
       System.out.print("Enter the value: ");
       Scanner sc = new Scanner(System.in);
       int value = sc.nextInt();
       //Calculates the factorial, and prints the result to the screen.
       System.out.println("The factorial of " + value + " is: " + factorial(value));
    }
    //Function to calculate the factorial of a number.
    public static long factorial(int n)
    {
       if(n <= 0)
           return 1;
       return n * factorial(n-1);  
    }
}​

And the output screenshot is:

Add a comment
Know the answer?
Add Answer to:
Java program 5. Write a short Java program to compute factorial of a given integer from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT