Function Factorial - C# Programming Exercise

In this exercise of C#, you will learn how to create a recursive function to calculate the factorial of a number. The factorial of a number is expressed as follows: n! = n · (n-1) · (n-2) · (n-3) · ... · 3 · 2 · 1. For example, the factorial of 6 is calculated as 6·5·4·3·2·1, which results in 720. In this exercise, you will need to create a recursive function that receives a number as a parameter and calculates its factorial. This type of exercise is perfect for learning how recursive functions work in C# and how to perform complex calculations efficiently using recursion. The example shows that if the function Factorial is called with the parameter 6, the program will return 720.

Learn how to implement recursive functions in C# to perform advanced mathematical calculations with this hands-on exercise and enhance your programming skills.

 Category

Functions

 Exercise

Function Factorial

 Objective

The factorial of a number is expressed as follows:

n! = n · (n-1) · (n-2) · (n-3) · ... · 3 · 2 · 1

For example,
6! = 6·5·4·3·2·1

Create a recursive function to calculate the factorial of the number specified as parameter:

Console.Write ( Factorial (6) );

would display 720

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Main method where the program starts
    public static void Main()
    {
        // Calling the Factorial function with the number 6 and printing the result
        Console.WriteLine(Factorial(6)); // The factorial of 6 is 720, which will be printed
    }

    // Recursive function to calculate the factorial of a number
    public static int Factorial(int n)
    {
        // Base case: if n is 1 or less, return 1 (because 1! = 1 or 0! = 1)
        if (n <= 1)
        {
            return 1; // Factorial of 1 or less is 1
        }
        
        // Recursive case: n * factorial of (n-1)
        return n * Factorial(n - 1); // Multiply n by the factorial of (n-1)
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function Parameters of Main, Reverse

    In this exercise of C#, you will learn how to create a program named reverse that receives several words from the command line and display...

  •  Function GetInt

    In this exercise of C#, you will learn how to create a function named GetInt, which displays on screen the text received as a parameter, asks the user f...

  •  Function tasks database

    In this exercise of C#, you will improve a "tasks database" program by splitting it into multiple functions. This type of exercise is great for l...

  •  Function Greatest value in a array

    In this exercise of C#, you will learn how to create a function that returns the greatest value stored in an array of real numbers, which is specified as a par...

  •  Function factorial (iterative)

    In this exercise of C#, you will learn how to create an iterative (non-recursive) function to calculate the factorial of the number specified as a parameter. T...

  •  Function WriteTitle

    In this exercise of C#, you will learn how to create a function named "WriteTitle" that writes a text centered on the screen, in uppercase, with extra spaces, ...