Q:

Write a C program to calculate factorial using recursion

0

Write a C program to calculate factorial using recursion. Here’s a Simple Program to find factorial of a number using recursive methods in C Programming Language.

This Program prompts user for entering any integer number, finds the factorial of input number and displays the output on screen.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Factorial of a Number : :


A factorial of a number x is defined as the product of x and all positive integers below x.  A factorial is product of all the number from 1 to the user specified number.

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 exist. And the factorial of 0 is 1. You will learn to find the factorial of a number using recursion method in this example.

 

 Using Recursion : :


We will use a recursive user defined function to perform the task. Here we have a function fact( )  that calls itself in a recursive manner to find out the factorial of input number.

Below is the source code for C program to calculate factorial using recursion which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/* C program to calculate factorial using recursion */

#include<stdio.h>

long int fact(int n);

int main( )
{
        int num;
        printf("Enter any number :: ");
        scanf("%d", &num);

        printf("\nUsing Recursion ---> \n");
        if(num<0)
                printf("\nError! No factorial for negative number...\n");
        else
                printf("\nFactorial of [ %d! ] is :: %ld\n", num, fact(num) );

                return 0;
}/*End of main()*/

/*Recursive*/
long int fact(int n)
{
        if(n == 0)
                return(1);

        return(n * fact(n-1));
}/*End of fact()*/

OUTPUT  : :


/* C program to calculate factorial using recursion */

Enter any number :: 7

Using Recursion --->

Factorial of [ 7! ] is :: 5040

Process returned 0

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Recursion Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program for GCD of two numbers using recursion... >>
<< C program to Calculate Power of a Number using rec...