C Chapters

C program to find the factorial of given number using recursion.

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

int main()
{
    int n;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    printf("\nFactorial of %d = %ld", n, multiplyNumbers(n));
    return 0;
}
long int multiplyNumbers(int n)
{
    if (n >= 1)
        return n*multiplyNumbers(n-1); /*Recursion : Function call it self */
    else
        return 1;
}

Output :
Enter a positive integer: 5
Factorial of 5 = 120

Leave a comment
Recent Comments
No Comments