## C

O

D

E

**FACTORIAL USING RECURSION:**

**FACTORIAL USING RECURSION:**

#### CODE:

#include <stdio.h>

// Function to calculate factorial

int factorial(int num) {

if (num == 0 || num == 1)

return 1;

else

return num * factorial(num – 1);

}

int main() {

int num;

printf(“Enter a positive integer: “);

scanf(“%d”, &num);

// Check if the number is negative

if (num < 0) {

printf(“Error! Factorial of a negative number doesn’t exist.”);

} else {

printf(“Factorial of %d = %d”, num, factorial(num));

}

return 0;

}

OUTPUT:

Enter a positive integer: 5

Factorial of 5 = 120

### DOWNLOAD CODE:

## P R O G A M M I N G

#### Table of Contents

## E X E C U T E

## E X P L A N A T I O N

*FACTORIAL USING RECURSION:*

: This line includes the standard input-output library, allowing the program to use functions like`#include <stdio.h>`

`printf`

and`scanf`

.

`factorial`

Function: This function is responsible for calculating the factorial of a given number.*int factorial(int num)**{*

if (num == 0 || num == 1)

return 1;

else

return num * factorial(num – 1);

}

Explanation:

- The
`factorial`

function is defined to take an integer argument`num`

, which represents the number for which we want to calculate the factorial. - The function employs a recursive approach, meaning it calls itself with a smaller value until it reaches the base case.
- The base case is defined using the
`if`

statement:`if (num == 0 || num == 1) return 1;`

. In mathematical terms, the factorial of 0 and 1 is always 1. - If the input number is not 0 or 1, the function calculates the factorial using recursion. It multiplies the current number by the result of the
`factorial`

function called with`num - 1`

, effectively reducing the problem to calculating the factorial of a smaller number.

`main`

Function: This function handles user input, checks for invalid input, and calls the`factorial`

function.**int main() {**

**int num;****printf(“Enter a positive integer: “);**

**scanf(“%d”, &num);****if (num < 0) {**

**printf(“Error! Factorial of a negative number doesn’t exist.”);**

**} else {**

**printf(“Factorial of %d = %d”, num, factorial(num));**

**}****return 0;**

**}**

Explanation:

- The
`main`

function is the entry point of the program. - It declares an integer variable
`num`

to store the user’s input. - It uses the
`printf`

function to display a prompt, “Enter a positive integer,” to the user. - The
`scanf`

function reads the user’s input and stores it in the`num`

variable. - The program then checks whether the input is negative using an
`if`

statement. If it’s negative, it displays an error message as “Error! Factorial of a negative number doesn’t exist.” - If the input is non-negative (zero or positive), the program calculates the factorial using the
`factorial`

function and displays the result using`printf`

.

How the Program Works:

- The program starts by including the necessary library and defining the
`factorial`

and`main`

functions. - In the
`main`

function, it prompts the user to enter a positive integer. - The program reads the user’s input and stores it in the
`num`

variable. - It checks if the input is negative. If it is, the program displays an error message, stating that factorial of a negative number doesn’t exist.
- If the input is non-negative (zero or positive), the program calls the
`factorial`

function, passing`num`

as the argument. - The
`factorial`

function calculates the factorial using recursion and returns the result. - The result is displayed in the format “Factorial of
`num`

=`result`

.”

Recursion in Detail: Recursion is a programming technique where a function calls itself in order to solve a problem. In this program, recursion is used to calculate the factorial of a number. Here’s how it works step by step:

- The
`factorial`

function is called with an integer argument`num`

. - It checks if
`num`

is either 0 or 1. If so, it returns 1 because the factorial of 0 and 1 is 1 (base case). - If
`num`

is not 0 or 1, it proceeds to calculate the factorial. - It does this by multiplying
`num`

by the result of calling the`factorial`

function with the argument`num - 1`

. - This recursive call continues, reducing
`num`

by 1 in each iteration until it reaches the base case of 0 or 1. - At that point, the recursive calls stop, and the function starts returning values back up the call stack.
- Each return multiplies the current
`num`

by the result of the recursive call. This continues until the final result is obtained and returned to the`main`

function.

Consider an example: If the user enters 5, the recursive calls and calculations would look like this:

`factorial(5)`

calls`factorial(4)`

, which calls`factorial(3)`

, and so on.`factorial(1)`

returns 1 (base case).`factorial(2)`

returns`2 * 1 = 2`

.`factorial(3)`

returns`3 * 2 = 6`

.`factorial(4)`

returns`4 * 6 = 24`

.**Finally,**`factorial(5)`

returns`5 * 24 = 120`

.

So, the program displays “Factorial of 5 = 120” as the output.

Handling Negative Input: The program checks if the user inputs a negative number, and if so, it displays an error message. This is essential because the factorial is only defined for non-negative integers. Trying to calculate the factorial of a negative number would lead to incorrect results.

Summary: In summary, the provided C program demonstrates the use of recursion to calculate the factorial of a positive integer. It uses a recursive function to break down the problem into smaller subproblems until it reaches the base case. The main function handles user input, checks for negative input, and displays the calculated factorial. This program is a simple and effective illustration of recursion in programming.

**MORE PROGRAM :**

**MORE PROGRAM :**

- https://d-techsol.in/in-c-programming-write-a-code-of-lcm-of-two-number/
- https://d-techsol.in/write-a-program-of-addition-of-number-in-c/
- https://d-techsol.in/fibonacci-series-in-c-program-using-for-loop/
- https://d-techsol.in/easy-code-of-factorial-in-c-program/

* BEST C PROGRAMMING BOOK *: