Do you want to print the factorial of a number in C#? In this tutorial, I will explain how to write a C# program to print the factorial of a number. I will show you how to write a C# program to find the factorial program in c# without using loop.
To calculate the factorial of a number in C#, you can use an iterative approach with a for
loop. In the program, you first read the user’s input, then iterate from 2 up to the number, multiplying each value with a running total to obtain the factorial.
Write a C# Program to Print the Factorial of a Number
The factorial of a non-negative integer n
, denoted by n!
, is the product of all positive integers less than or equal to n
. For example, the factorial of 5 (5!) is 5 x 4 x 3 x 2 x 1 = 120.
We can calculate factorials in C# using either an iterative approach (using loops) or a recursive approach. For simplicity and better performance on larger numbers, we’ll use the iterative approach in this example.
Here’s the complete code to calculate the factorial of a number in C#:
using System;
class FactorialProgram
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number to calculate its factorial:");
int number = Convert.ToInt32(Console.ReadLine());
long factorial = CalculateFactorial(number);
Console.WriteLine($"The factorial of {number} is {factorial}.");
}
static long CalculateFactorial(int number)
{
if (number < 0)
{
throw new ArgumentException("Factorial of a negative number doesn't exist.");
}
long result = 1;
for (int i = 2; i <= number; i++)
{
result *= i;
}
return result;
}
}
Here is the complete code explanation:
- We start by importing the
System
namespace which is essential for basic input and output operations. - The
FactorialProgram
class contains theMain
method, which is the entry point of the program. - Inside
Main
, we prompt the user to enter a number and read this input usingConsole.ReadLine()
. - The
CalculateFactorial
method is then called to compute the factorial of the entered number. This method uses a loop to calculate the factorial iteratively. - The program handles negative inputs by throwing an
ArgumentException
, as factorials for negative numbers are undefined. - Finally, the factorial is printed to the console.
Once you run the code, it will ask you to enter a number, and then it will display the factorial of the number in the C# console application. Here is the screenshot for the output:
Factorial program in c# without using loop
If you are looking to find the factorial of a number in C# without using loop, then check out the below program. The program is to get factorial program in c# without using loop.
In C#, apart from the iterative method for calculating the factorial of a number, you can also use the recursive approach. Recursive methods call themselves with a reduced parameter until they reach a base case. In the case of factorial calculation, the base case is when the number is 0 or 1, as the factorial of both is 1.
using System;
class FactorialProgram
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number to calculate its factorial:");
int number = Convert.ToInt32(Console.ReadLine());
long factorial = CalculateFactorialRecursive(number);
Console.WriteLine($"The factorial of {number} is {factorial}.");
}
static long CalculateFactorialRecursive(int number)
{
if (number < 0)
{
throw new ArgumentException("Factorial of a negative number doesn't exist.");
}
if (number == 0 || number == 1)
{
return 1;
}
return number * CalculateFactorialRecursive(number - 1);
}
}
Once you run the code using the Visual Studio C# console application, you can see the output like the screenshot below.
Conclusion
This C# program is a basic demonstration of calculating factorials. It is an excellent example for beginners to understand loops and basic exception handling in C#. In this C# tutorial, I have explained how to write a C# program to print the factorial of a number.
You may also like:
- Write A C# Program To Find The Largest Of Three Numbers
- Write A C# Program To Print The Multiplication Table Of A Number
- Write a C# Program to Print All Natural Numbers in Reverse Order
- Write a C# Program to Print Alphabet Triangle
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…