C# Factorial - c# - c# tutorial - c# net



Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

How to write Factorial Program in C# ?

  • Factorial of a number is obtained from the result of multiplying a series of descending natural numbers. Here is source code of the C# Program to Generate the Factorial of Given Number.
  • Factorial of n is the product of all positive descending integers.
  • Factorial of n is denoted by n!.

For example:

4! = 4*3*2*1 = 24    
6! = 6*5*4*3*2*1 = 720 
click below button to copy the code. By - c# tutorial - team
  • Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek".
  • The factorial is normally used in Combinations and Permutations (mathematics).
  • Let?s see the factorial program in C# using for loop.

Example1:

using System;  
 public class FactorialExample  
   {  
   	  public static void Main(string[] args)  
      {  
       int i,fact=1,number;      
       Console.Write("Enter any Number: ");      
       number= int.Parse(Console.ReadLine());     
       for(i=1;i<=number;i++){      
        fact=fact*i;      
       }      
       Console.Write("Factorial of " +number+" is: "+fact);    
     }  
  } 
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

Enter any Number: 6
Factorial of 6 is: 720

Example2:

/*
 * C# Program to Generate the Factorial of Given Number 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, number, fact;
            Console.WriteLine("Enter the Number");
            number = int.Parse(Console.ReadLine());
            fact = number;
            for (i = number - 1; i >= 1; i--)
            {
                fact = fact * i;
            }
            Console.WriteLine("\nFactorial of Given Number is: "+fact);
            Console.ReadLine();
 
        }
    }
}
click below button to copy the code. By - c# tutorial - team
Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

output:

Enter the Number
5
Factorial of Given Number is: 120


Related Searches to C# Factorial