(USING STUDIO VISUAL) }Factorial: The factorial of an integer n, written n!, is
ID: 2085654 • Letter: #
Question
(USING STUDIO VISUAL)
}Factorial:
The factorial of an integer n, written n!, is the product of n*(n-1)*(n-2)… until you get to 1. For example, 3! = 3*2 = 6, and 4! = 4*3*2 = 24. Factorial tells the number of permutations of n items – for example, the # of ways you can deal an entire deck of cards = 52!, which is huge.
DESIGN a program that:
•Prompts the user to enter an integer greater than 0.
•Uses a while() loop to calculate the factorial of the integer, and prints it out along with the original integer.
•Uses a long int to hold the factorial, to prevent overflow.
Example output (with user input shown underlined):
Factorial program: enter an integer greater than 0: 5
5 factorial (5!) is 120.
Explanation / Answer
## I don't have visual studio on myself, but I've worked this on an online C# compiler. Check the alternative solution if this shows an error ##
##Solution 1
using System;
class FindFactorialWhileLoop
{
static void Main()
{
{
Console.Write("Enter your factorial number = ");
long n = long.Parse(Console.ReadLine());
decimal result = 1;
while (true)
{
Console.Write(n);
if (n == 1)
{
break;
}
Console.Write(" * ");
result *= n;
n--;
}
Console.WriteLine(" = {0}", result);
}
}
}
## Solution 2
using System;
class FindFactorialWhileLoop
{
static void Main()
{
{
Console.Write("Enter your factorial number = ");
decimal n = decimal.Parse(Console.ReadLine());
decimal result = 1;
while (true)
{
Console.Write(n);
if (n == 1)
{
break;
}
Console.Write(" * ");
result *= n;
n--;
}
Console.WriteLine(" = {0}", result);
}
}
}
## Here I used decimal type to hold the factorial instead of long int.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.