w3resource logo
C Programming Exercises

C Exercises: Find the Factorial of a number

C Recursion : Exercise-10 with Solution

Write a program in C to find the Factorial of a number using recursion.

Sample Solution:

C Code:

#include<stdio.h>
int findFactorial(int);

int main()
{
  int n1,f;
	printf("\n\n Recursion : Find the Factorial of a number :\n");
	printf("-------------------------------------------------\n");	  
  printf(" Input  a number : ");
  scanf("%d",&n1);
  f=findFactorial(n1);//call the function findFactorial for factorial
  printf(" The Factorial of %d is : %d\n\n",n1,f);
  return 0;
}

int findFactorial(int n)
{
   if(n==1)
       return 1;
   else
       return(n*findFactorial(n-1));// calling the function findFactorial to itself recursively
 }

Sample Output:

 Recursion : Find the Factorial of a number :                                                                 
-------------------------------------------------                                                             
 Input  a number : 5                                                                                          
 The Factorial of 5 is : 120 

Flowchart:

Flowchart: Find the Factorial of a number.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.