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:
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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework