Question

JAVA: Create a Factorial application that prompts the user for a number and then displays its...

JAVA: Create a Factorial application that prompts the user for a number and then displays its factorial. The factorial of a number is the product of all the positive integers from 1 to the number. For example, 5! = 5*4*3*2*1.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java program for calculating the factorial using recursion

import java.util.*;

public class factorial {

public static int func (int n) //function for calculating factorial

{

if(n==0||n==1) //base condition of recursion since 0!=1!=1

return 1;

else

return n*func(n-1); // this is recursion condition which says that multiplication will continue till n reaches base condition

}

public static void main(String[] args)

{

int x; //variable for accepting the number whose factorial is to be calculated

Scanner sc=new Scanner(System.in); //object of scanner class

System.out.println("Enter the number whose factorial is to be calculated:");

x=sc.nextInt(); //accepting the number whose factorial is to be calculated

System.out.println(x+"! = "+func(x)); //printing the result

}

}

Add a comment
Know the answer?
Add Answer to:
JAVA: Create a Factorial application that prompts the user for a number and then displays its...
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