

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# factorial
To calculate factorial in C#, you can use while loop and loop through until the number is not equal to 1.
Here n is the value for which you want the factorial −
int res = 1; while (n != 1) { res = res * n; n = n - 1; }
Above, let’s say we want 5! (5 factorial)
For that, n=5,
Loop Iteration 1 −
n=5 res = res*n i.e res =5;
Loop Iteration 2 −
n=4 res = res*n i.e. res = 5*4 = 20
Loop Iteration 3 −
n=3 res = res*n i.e. res = 20*3 = 60
Example
In this way, all the iterations will give the result as 120 for 5! as shown in the following example.
using System; namespace MyApplication { class Factorial { public int display(int n) { int res = 1; while (n != 1) { res = res * n; n = n - 1; } return res; } static void Main(string[] args) { int value = 5; int ret; Factorial fact = new Factorial(); ret = fact.display(value); Console.WriteLine("Value is : {0}", ret ); Console.ReadLine(); } } }
Output
Value is : 120
- Related Questions & Answers
- factorial() in Python
- Clumsy Factorial in Python
- Factorial recursion in JavaScript
- Calculate Factorial in Python
- Inverse Factorial in Python
- Factorial of a large number
- C++ Program to Find Factorial
- Recursive factorial method in Java
- Factorial Trailing Zeroes in C++
- Find the last digit when factorial of A divides factorial of B in C++
- Factorial program in Java using recursion.
- Calculating factorial by recursion in JavaScript
- Factorial program in Java without using recursion.
- Python Program for factorial of a number
- Count digits in a factorial in C++
Advertisements