
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Three Different ways to calculate factorial in C#
To calculate a factorial in C#, you can use any of the following three ways −
Calculate factorial with for loop
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace factorial { class Test { static void Main(string[] args) { int i, res; int value = 5; res = value; for (i = value - 1; i >= 1; i--) { res = res * i; } Console.WriteLine("
Factorial of "+value+" = "+res); Console.ReadLine(); } } }
Output
Factorial of 5 = 120
Calculate factorial with a while loop
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
Calculate factorial using recursion
Example
using System; namespace MyApplication { class Factorial { public int display(int n) { if (n == 1) return 1; else return n * display(n - 1); } 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 Articles
- Calculate Factorial in Python
- Calculate the Estimate at Completion (EAC) in Different Ways
- Different Ways to Add Parentheses in C++
- Write a C# program to calculate a factorial using recursion
- C++ program to Calculate Factorial of a Number Using Recursion
- Different ways to start a Task in C#
- List at least three different ways in which 67 be expressed as the sum of three different prime numbers
- Different ways for Integer to String Conversions in C#
- Different ways to declare variable as constant in C and C++
- Different ways of Reading a file in C#
- Print system time in C++ (3 different ways)
- How to calculate power of three using C#?
- C program to calculate distance between three points in 3D
- Number of Ways to Wear Different Hats to Each Other in C++
- Different ways to concatenate Strings in Java

Advertisements