- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming
Recursive C Program To Find The Factorial of a Number
Recursion and Factorial :
What is Recursion in C Program :
Recursion refers to Calling the function again and again inside the body of any Program by the Same Function. In other words, It refers to thinking and then solving the problem. For Each Call of a function, the Program computes better result.
With Recursion , The Problem in C Programming can be reduced in each function call till the base value or result is reached beyond which no Call can be made or no more better result can be obtained.
Factorial of a Number :
Factorial of any number which should be non negative is equal to Product of all the integers less than or equal to that particular number. It is denoted by n! where n is any positive number and its factorial will be equal to :
n!=n*(n-1)*(n-2)*(n-3)... and so on till integer value 1 comes.
Let us take the example of 5! . It will be equal to 5*4*3*2*1 => 120 .
0! according to number systems is treated as 1. In this program, we shall use Recursion technique to find the Factorial of any positive number. Copy the Source code and save it as *.c . Compile and run thereafter the C Program.
C Program Source Code To Find Factorial of a Number :
#include<stdio.h> #include<conio.h> //recursive function int fact(int n) { if(n==1) return 1; else return(n*fact(n-1)); } //main function void main() { int n,f; printf("Enter the number: "); scanf("%d",&n); if(n==0) printf("FACTORIAL: 1"); else { f=fact(n); printf("FACTORIAL: %d",f); } getch(); }