Home About Contact Privacy Terms Disclaimer
TECHAntena
Last Updated On: June 6, 2017
Editor: Adeeb C

Algorithm to find factorial of a number using recursion with C program

Category: C,Programing
Tags

This is the C program code and algorithm to finding factorial of a given number using recursion.

Factorial using Recursion

Aim:

Write a C program to find the factorial of a given number using recursion.

Algorithm:

Step 1: Start
Step 2: Read number n
Step 3: Call factorial(n)
Step 4: Print factorial f
Step 5: Stop
factorial(n)
Step 1: If n==1 then return 1
Step 2: Else 
f=n*factorial(n-1)
Step 3: Return f

Program code

#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( )
int factorial(int);
int n,f;
printf("Enter the number: ");
scanf("%d",&n);
f=factorial(n);
printf("Factorial of the number is %d",f);
getch();
}
int factorial(int n)
{
int f;
if(n==1)
return 1;
else
f=n*factorial(n-1);
return f;
}

Output

Enter the number : 5
Factorial of the number is 120