Home / Programs / C Program - Factorial of a Number Using Recursion
🚀 Programming Example

C Program - Factorial of a Number Using Recursion

👁 910 Views
💻 Practical Program
📘 Step Learning
Factorial of a Number Using Recursion:

The factorial of a positive number n is given by:

factorial of n (n!) = 1*2*3*4....n

The factorial of a negative number doesn't exi

💻 Program Code

#include <stdio.h>
long int multiplyNumbers(int n);

int main()
{
    int n;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    printf("Factorial of %d = %ld", n, multiplyNumbers(n));
    return 0;
}
long int multiplyNumbers(int n)
{
    if (n >= 1)
        return n*multiplyNumbers(n-1);
    else
        return 1;
}
                        

🖥 Program Output

Enter a positive integer: 6
Factorial of 6 = 720
                            

📘 Explanation

Suppose the user entered 6.

Initially, the?multiplyNumbers()?is called from the?main()?function with 6 passed as an argument.

Then, 5 is passed to the?multiplyNumbers()?function from the same function (recursive call). In each recursive call, the value of argument?n?is decreased by 1.

When the value of?n?is less than 1, there is no recursive call.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.