c-sharp
  1. c-sharp-program-to-find-factorial-of-a-number

Program to Find Factorial of a Number - (C# Basic Programs)

Calculating the factorial of a number is a common programming exercise that helps improve problem-solving skills. In this tutorial, we will discuss a program to find the factorial of a number using C# programming language.

Syntax

The syntax for finding the factorial of a number in C# is as follows:

int factorial = 1;
for (int i = 1; i <= num; i++)
{
    factorial = factorial * i;
}

Example

using System;

namespace Factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, i, fact;

            Console.Write("Enter a number: ");
            num = int.Parse(Console.ReadLine());

            fact = 1;
            for (i = 1; i <= num; i++)
            {
                fact = fact * i;
            }

            Console.WriteLine("Factorial of " + num + " is: " + fact);
            Console.ReadLine();
        }
    }
}

Output:

Enter a number: 5
Factorial of 5 is: 120

Explanation

A factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 5 (represented by 5!) equals 5 x 4 x 3 x 2 x 1 = 120. To find the factorial of a number in C#, we use a loop to multiply all the integers from 1 to that number. The loop starts at 1 and continues until it reaches the input number.

Use

Calculating the factorial of a number is a fundamental programming exercise and is used in many applications, such as mathematical modeling, probability theory, and algebraic equations. It is also an excellent exercise for practicing problem-solving and developing intermediate programming skills.

Summary

In this tutorial, we discussed a C# program to find the factorial of a number. We provided the syntax, an example, explanation, and use of the program. By practicing such exercises, programmers can improve their problem-solving skills and become better equipped to tackle complex coding problems.

Published on: