Showing posts with label Factorial. Show all posts
Showing posts with label Factorial. Show all posts

Sunday 31 July 2016

Factorial of a Number using Recursion

Recursion: Factorial 


Write a program to find a Factorial of a Given Number using Recursion.

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

void main()
{
        int n;
        long int f;
        printf("\nEnter the value of n: ");
        scanf("%d", &n);
        f = fact(n) ;
        printf("\nFactorial of given number %d is %ld", n, f);
}

long int fact(int n)
{
        if( n == 0)
                 return 1;
        else
                 return n*fact(n-1);
}

Output:
Enter the value of n: 5

Factorial of given number 5 is 120