This program takes a positive integer from user and calculates the factorial of that number. Suppose, user enters 6 then,
Factorial will be equal to 1*2*3*4*5*6 = 720
You’ll learn to find the factorial of a number using a recursive function in this example.
Visit this page to learn, how you can use loops to calculate factorial.
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace simpleCalc { class Program { static void Main(string[] args) { Console.WriteLine("\nRecursion : Find the factorial of a given number :"); Console.WriteLine("-------------------------------------------------------"); Console.Write(" Input any positive number : "); int n1 = Convert.ToInt32(Console.ReadLine()); long fact = FactCalc(n1); Console.WriteLine(" The factorial of {0} is : {1} ", n1, fact); Console.ReadKey(); } private static long FactCalc(int n1) { if (n1 == 0) { return 1; } return n1 * FactCalc(n1 - 1); } } } |
Output: