Sunday, December 6, 2020

What is Factorial program in C#?

Factorial program in C#

Factorial Program in C#: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:

  1. 4! = 4*3*2*1 = 24    
  2. 6! = 6*5*4*3*2*1 = 720      

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.

  1. using System;  
  2.   public class FactorialExample  
  3.    {  
  4.      public static void Main(string[] args)  
  5.       {  
  6.        int i,fact=1,number;      
  7.        Console.Write("Enter any Number: ");      
  8.        number= int.Parse(Console.ReadLine());     
  9.        for(i=1;i<=number;i++){      
  10.         fact=fact*i;      
  11.        }      
  12.        Console.Write("Factorial of " +number+" is: "+fact);    
  13.      }  
  14.   }  

Output:

Enter any Number: 6
Factorial of 6 is: 720

No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...