Question
The the factorial of an integer n is the product of all numbers from 1 to n and is written n. So 5! = 5
The the factorial of an integer n is the product of all numbers from 1 to n and is written n. So 5! = 5 * 4 * 3 * 2 * 1 = 120 By definition, 0! = 1.
Write a program that asks the user for an integer. If the integer is greater than or equal to 0, the program calculates the factorial of that number and prints that value. If the number entered is negative, the program prints an error message and quits.
Main Method:
The main method must create a Scanner object, ask the user for an integer, and use the Scanner object to get a input from the user. If the input is negative, an error message is printed. If the input is not negative, the main method should call the factorial method to get the factorial of the integer entered by the user. And print the value returned by the factorial method.
Factorial Method:
The factorial method must take an integer as its parameter and return an integer. The method should calculate the factorial of the value and return it.
Your output should look something like this:
Please enter an integer greater than or equal to zero: 0
Output: The factorial of 0 is 1
Please enter an integer greater than or equal to zero: 1
Output: The factorial of 1 is 1
Please enter an integer greater than or equal to zero: 6
Output: The factorial of 6 is 720
Please enter an integer greater than or equal to zero: -3
Output: You cannot take the factorial of a negative number.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started