C-SHARP - 3.13 Practice

In this exercise we summarise everything seen so far: variables, console input, parsing, conditions and loops. You must build a console program that asks the user for a number, computes the factorial of that number and displays the result. A factorial is the product of all positive integers less than or equal to a number n, for example 5! = 1 * 2 * 3 * 4 * 5 = 120. To stay inside the int range, only accept numbers less than or equal to 31; otherwise print a message saying the number is too large.

The first step is to prompt the user with Console.WriteLine, then read the answer with Console.ReadLine which returns a string. Since we need to do arithmetic, we convert with int.Parse to obtain an int stored in a variable named conversion. The heart of the program is a for loop that builds the product. We declare a factorial variable initialised to 1 (initialising to 0 would always yield 0) then iterate from i = 1 while i <= conversion and multiply at each step.

Solution

  • Read the user input as string and parse it with int.Parse.
  • Initialise factorial = 1 and loop for (int i = 1; i <= conversion; i++).
  • Inside the loop write factorial = factorial * i;.
  • Wrap the display in an if (conversion <= 31), otherwise warn the user.
Console.WriteLine("You want the factorial of which number?");
string numberUser = Console.ReadLine();
int conversion = int.Parse(numberUser);

int factorial = 1;
for (int i = 1; i <= conversion; i++)
{
    factorial = factorial * i;
}

if (conversion <= 31)
{
    Console.WriteLine(string.Format("The factorial of {0} is {1}", numberUser, factorial));
}
else
{
    Console.WriteLine("The number is too large, we leave the int.");
}

Testing with 5 displays 120, with 6 we get 720, and with 50 the else branch warns that the value is out of range. You can also place the for loop directly inside the if block to avoid unnecessary computation. As a developer, always strive to ask the compiler for as few operations as possible to keep your programs optimised and clear.

Summary

This lesson covers a comprehensive C-sharp practice exercise on calculating factorials. Students are tasked with creating a program that takes a user-entered number and computes its factorial (e.g., 5! = 1×2×3×4×5 = 120), with proper data type handling (int for numbers up to 31, long for larger values). The lesson then walks through the complete solution, demonstrating Console.ReadLine() for input, int.Parse() for string-to-integer conversion, a for loop for factorial multiplication, and conditional logic to enforce the 31-number limit.

Key points

  • Factorial definition: the product of all positive integers from 1 to n (e.g., 5! = 1×2×3×4×5 = 120)
  • Use Console.ReadLine() to capture user input as a string, then int.Parse() to convert to integer
  • Initialize factorial variable to 1 (not 0) to avoid zeroing the product during multiplication
  • Implement a for loop (i = 1; i ≤ user_number; i++) where factorial = factorial × i at each iteration
  • Add conditional check: if user number ≤ 31 display factorial; otherwise display 'number too large, exceeds int range'

FAQ

Why initialize factorial to 1 instead of 0?

Because any number multiplied by 0 equals 0, which would give an incorrect result. Starting at 1 preserves the product through each multiplication iteration (1 × 1 = 1, 1 × 2 = 2, 2 × 3 = 6, etc.).

Why is there a limit of 31 for int type in this exercise?

Because factorial values grow extremely fast; 32! exceeds the maximum value an int can hold in C-sharp, requiring the long data type instead.

Why use int.Parse() and not Console.ReadLine() directly in the loop?

Because Console.ReadLine() returns a string, and you cannot perform arithmetic operations directly on strings. Conversion to int is mandatory before calculations.