×

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

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.