w3resource logo


C Programming Exercises

C Exercises: Find the Factorial of a number

Secondary Nav

C Recursion : Exercise-10 with Solution

Write a program in C to find the Factorial of a number using recursion.
Test Data :
Input a number : 5
Expected Output :

The Factorial of 5 is : 120 

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
 }

Flowchart :

Flowchart: Find the Factorial of a number.

C Practice online:

#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
 }

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number