Create a C# console application for the follow problem. Factorial of a non-negative integer n (i.e. n!) is defined as follows: n! = 1 if
Create a C# console application for the follow problem.
Factorial of a non-negative integer n (i.e. n!) is defined as follows:
n! = 1 if n = 0
n! = 1 * 2 * 3 * 4 * * (n-1) * n if n >= 1
For example,
4! = 1 * 2 * 3 * 4 = 24
Write a C# program to calculate factorial. The user enters a positive integer n. The program will calculate and display n!. Your program should be able to handle three types of exceptions.
FormatException: This happens when the user enters something that cannot be converted to int type value. When this type of exception happens, display the error message Invalid number format in the console window and ask the user to try again. Example:
Enter a non-negative integer: 4.5
Invalid number format. Please try again.
NegativeIntegerException: This happens when the user tries to find the factorial of a negative integer. The .NET exception hierarchy does not have this exception. You must create it yourself. When this type of exception happens, display the error message Factorial of negative integer not permitted in the console window and ask the user to try again. Example:
Enter a non-negative integer: -4
Factorial of negative integer not permitted. Please try again.
OverflowException: This happens when the factorial is too large. The value of factorial increases rapidly. An overflow happens for n > 20 even if you use a long type variable to hold the factorial. To detect an overflow, you can use checked. Example:
long f = 0;
f = checked (a * b);
If the result of a * b is larger than what f can store, an overflow exception will be thrown.
Display the error message Input value too large in the console window and ask the user to try again if an overflow exception happens. Example:
Enter a non-negative integer: 40
Input value too large. Please try again.
The following figure shows various types of overflow caught.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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