Question
Finding the Factorial The factorial of a number (written as n!) is defined as the product of the integers between 1 and n. For example:
Finding the Factorial
The factorial of a number (written as n!) is defined as the product of the integers between 1 and n. For example: 4! = 1 * 2 * 3 * 4 = 24. By definition, 0! = 1. The factorial is not defined for negative numbers.
Write a Java program that asks the user for a non-negative integer and computes and prints the factorial of that number. Your code should verify that the number entered is nonnegative. The code should also produce the value of 1 if 0 is entered as the number.
Your program should include:
The Scanner class for input
While or do while loops for input validation
A while loop to compute the factorial
Samples of the output is shown below:
Output 1:
Please enter a nonnegative integer: -5 Factorial is undefined for negative numbers. Please enter a non-negative integer: 5 5! = 120
Output 2:
Please enter a nonnegative integer: 0 0! = 1
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