Factorial is a mathematical operation that is used quite often in programming. It involves multiplying a given number by all positive integers that are less than it. In this article, we'll explore how to write a C# program to calculate the factorial of a given number.
Problem Statement: Write a C# program that calculates the factorial of a positive integer.
Example:
Input: num = 5 Output: Factorial of 5 = 120 Explanation: 5 x 4 x 3 x 2 x 1 = 120 Input: num = 7 Output: Factorial of 7 = 5040
Steps to Find Factorial of a Number.
C# Code to Find Factorial of a Number.
// C-sharp code to find factorial of a number. using System; namespace FactorialCalculator { class Program { static void Main(string[] args) { Console.Write("Enter a positive integer: "); int number = Convert.ToInt32(Console.ReadLine()); int factorial = 1; for (int i = 1; i <= number; i++) { factorial *= i; } Console.WriteLine($"Factorial of {number} is {factorial}."); } } }
Enter a positive integer: 7 Factorial of 7 is 5040.

