×

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean?

In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and it is symbolised by that positive integer and an exclamation point. So, factorial seven is represented as 9!, which equals 9*8*7*6*5*4*3*2*1. Factorial 0 is equal to 1 in mathematics.

Example:

  12! =12*11*10*9*8*7*6*5*4*3*2*1
  17! =17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1

To find factorial of a given number in C, there are two ways:

  1. Using Recursion
  2. Using Loop

Factorial of a Number using Recursion:

This code asks the user to enter any integer number, calculates its factorial, and displays the results on the screen. To complete the work, we will utilize a recursive function which must be user defined. In order to determine the factorial of the input integer, the function "factorial" in this case calls itself recursively. The code includes user interaction, but if we don't want it, we can just provide an integer number to variable no and omit the scanf statement. In other words, we can change it as we like; the argument would remain the same in every situation.

Code:

// using recursion


#include<stdio.h>  
  
long long int factorial ( long int no )  
{  
  	if ( no == 0 )  //check whether the condition is true or false
    	return 1;  //if the number is zero it will return one
    	
	long long int fact = factorial ( no - 1 ); //storing the value of the recursive function in variable
    
	return no * fact;  //returning the value
}  
   
int main()  
{  
  long long int numb; //taking variable numb
  long long int f;  //taking variable f
  printf("Enter a number: ");  //put any integer number
  scanf( "%ld", & numb) ;  //value stores in numb variable 
   
  f = factorial ( numb );  //f stores the value of the factorial
  printf( "%ld is the factorial of %ld\n", f, numb );  //printing the factorial value of a number
  return 0;  
}

Output: (If user puts 9 as input)

Enter a number: 9
362880 is the factorial of 9

Output: (If user puts 11 as input)

Enter a number: 11
39916800 is the factorial of 11

Note: Remember that we can not find factorial of a large number as the value will be so large to store.

C Program to find factorial of a given number using loop:

Code:

//using loop


#include<stdio.h>  
int main()    
{    
 long long int index, facto=1, no;   //taking variables 
 printf("Enter a number: ");    //asks user for a integer value
  scanf("%ld", & no);    //stores value in "no" variable
    for (index = 1; index <= no; index ++) //using for loop
	{    
      facto = facto * index;   // logic
    }    
   
     printf("%ld is the factorial of %ld\n", facto, no );  //printing the factorial value of given number  
     return 0;  
}  

Output: (If user puts 12 as input)

Enter a number: 12
479001600 is the factorial of 12

Output: (If user puts 4 as input)

Enter a number: 4
24 is the factorial of 4

Related Topics

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.