The Program in C# Program to Calculate Factorial of Given Number is given below:
using System;
namespace FactorialCalculator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family, enter a number here to calculate its factorial: ");
int number = int.Parse(Console.ReadLine());
int result = 1;
for (int i = 1; i <= number; i++)
{
result *= i;
}
Console.WriteLine("Okay, the factorial of " + number + " is " + result);
Console.ReadLine();
}
}
}
Output:
Hello Codeauri Family, enter a number here to calculate its factorial: 5
Okay, the factorial of 5 is 120
Pro-Tips💡
Here are the step by step execution of above program:
- The code starts by using the System namespace which provides access to various functionalities including input/output operations through the Console class.
- The code defines a namespace named “FactorialCalculator”.
- The code defines a class named “Program” within the namespace.
- The Main method is defined within the Program class, which is the entry point of the program.
- The code displays a message asking the user to enter a number.
- The entered value is read as a string, parsed into an integer, and stored in the “number” variable.
- The “result” variable is initialized to 1.
- A for loop starts from 1 and goes until “number”. In each iteration, the value of “result” is multiplied by the loop counter “i”.
- After the loop, the code displays the result by writing a message to the Console.
- The Console.ReadLine() method is called at the end of the Main method to prevent the program from terminating immediately after printing the result.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.