Courses

### Courses

Structure and Union

No Examples found for this topic - CodeHelpPro

Data Files

No Examples found for this topic - CodeHelpPro

# Factorial of a Number using Recursion

0 June 21, 2021

In this example, We will learn a program to Factorial of a Number using Recursion. Before that, You must have knowledge of the following topics.

Make sure you have knowledge of Recursive Function.

Now, we will look on algorithm and flowchart and we will write flowchart of plain program. i.e without using recursive function.

We can also find the factorial without using recursive function or any functions. Below is the code

``````#include<stdio.h>

int main(){

int num, fact=1, i;
printf("Enter a number: ");
scanf("%d", &num);

for( i = num; i > 0; i-- ){
fact *= i;
}
printf("\nFactorial of  %d is  %d", num, fact);
return 0;

}``````

## Algorithm

The algorithm of above program is

```Step 1: Start
Step 2: Declare Variable n, fact, i
Step 3: Read number from User
Step 4: Initialize Variable fact=1 and i=num
Step 5: Repeat Until i > 0
fact=fact*i
Decrease i by 1
Step 6: Print fact
Step 7: Stop```

## Flowchart

This is the flowchart of this program

## CODE: Factorial of a Number using Recursion

Now, We will learn a code to find Factorial of a any Number using Recursion.

``````#include<stdio.h>

//Function Prototype
int fact(int);

int main(){

int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("\nFactorial of  %d is  %d", num, fact(num));
return 0;

}

int fact(int a){

if( a == 1 || a == 0 )
return 1;
else
return ( a * fact( a - 1 ) );

}``````

The output of above program is

``````Enter a number: 5
Factorial of  5 is  120``````
Subscribe
Notify of
Inline Feedbacks