w3resource
C# Sharp Programming Exercises

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?



New Content: Composer: Dependency manager for PHP, R Programming