Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Calculate the factorial of a given number

C# Sharp For Loop: Exercise-15 with Solution

Write a C# Sharp program to calculate the factorial of a given number.

Pictorial Presentation:

C# Sharp Exercises: Calculate the factorial of a given number

Sample Solution:

C# Sharp Code:

using System;  
public class Exercise15  
{  
    public static void Main()
{
  int i,f=1,num;
  
	Console.Write("\n\n");
    Console.Write("Calculate the factorial of a given number:\n");
    Console.Write("--------------------------------------------");
    Console.Write("\n\n");  

  Console.Write("Input the number : ");
   num= Convert.ToInt32(Console.ReadLine()); 
  for(i=1;i<=num;i++)
      f=f*i;

  Console.Write("The Factorial of {0} is: {1}\n",num,f);
 }
}

Sample Output:

Calculate the factorial of a given number:                                                                    
--------------------------------------------                                             
Input the number : 6                                                                                          
The Factorial of 6 is: 720

Flowchart:

Flowchart: Calculate the factorial of a given number

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to make such a pattern like a pyramid with an asterisk.
Next: Write a program in C# Sharp to display the n terms of even natural number and their sum.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.