Factorial program in C with logic and examples using loops ( for, while and do while), functions and Recursion techniques.

For example, factorial of a number 5 is 120 using below factorial formula.

Factorial Formula:

n! = n × (n − 1) × (n − 2) . . . × 2 × 1 for a number n.

Example, factorial of 5 = (5! =5*4*3*2*1=120).

NOTE:

  • factorial of 0 = 0! =1
  • No factorial for negative number

Logic:

Logic is very simple, to find a factorial of a number in C programs, just multiply the number with all its descendant numbers till 1. For example, if number is 5 then factorial = 5*4*3*2*1.

Factorial programs in C

Factorial program in C using for loop

Simple Steps

  • Prompt user to enter a number to find factorial using printf function
  • Read the number and store into a variable, say num using scanf function
  • Declare an int variable factorial which will store the result and initialize it to 1.
  • Us a for loop, and run it from i=1 to i< =num.
  • In every pass, multiply the value of variable factorial with value of i. When loop ends the factorial variable will contains the final factorial value of given number.

Loop explained to find factorial

for (i = 1; i <= num; i++) {

            factorial = factorial * i;

      }

Consider that you need to find the factorial of 5. Below is the code for every pass in the loop

//5! = 5*4*3*2*1//factorial =1

	//i=1, factorial = factorial * i = 1*1 =1
	//=> factorial =1
	//i=2, factorial * i = 1 * 2 = 2
	//factorial = 2

	//i=3,factorial * i = 2 * 2 = 6
	//factorial =6

	//i=4,factorial * i = 6 * 4 = 24
	//factorial = 24

	//i=5,factorial * i = 24 * 5 = 120
	//factorial = 120

C code:

/* --------------------------------------------------
* C Program to find factorial of a number
*/

#include<stdio.h>
int main()
{
	int num, i, factorial;

	//initialize factorial by 1
	factorial = 1;

	printf("Enter a number to get factorial: ");

	//Read the number from user / console screen
	scanf("%d",&num);

	if(num < 0){
		//Return as there is no factorial of -ve numbers
		return 0;
	}

	for (i = 1; i <= num; i++) {

		factorial = factorial * i;
	}

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

	return 0;
}

Factorial program in C using while loop

Logic will be same as we used in for loop program. For simplicity, value are initialized with hard code value like num =5 and while loop has been used in the below C program.

/* -------------------------------------------------------
* C Program to find factorial of a number using while loop
*/

#include<stdio.h>
int main()
{
	int num =5;
	int i=1;
	int factorial =1;

	while(i <= num){

		factorial = factorial * i;
		++i;
	}

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

	return 0;
}

Factorial program in C using do while loop

This C program uses do while loop but the body will be same as in while loop.

/* -------------------------------------------------------
* C Program to find factorial of a number using Do while loop
*/
#include<stdio.h>

int main()
{
	int num =5;
	int i=1;
	int factorial =1;

	do{
		factorial = factorial * i;
		++i;
	}while(i <= num);

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

	return 0;
}

Factorial program in C using function

We will create a function e.g. getFactorial that will accept the given number as an argument, calculate the factorial using same logic as we use in loops and return the result.

We have defined the function above the main () program. If you wish to define the factorial function below the main () program then, you need to declare the function above the main() as below.

int getFactorial(int num);

C Code:

/* -------------------------------------------------------
* C Program to find factorial of a number using function
*/
#include<stdio.h>
//Define a function that returns a factorial of a number

int getFactorial(int num){

	int i;
	int factorial =1;

	if(num < 0){
		//Return as there is no factorial of -ve numbers
		return 0;
	}

	for (i = 1; i <= num; i++) {

		factorial = factorial * i;
	}

	return factorial;
}

//Test factorial function 
int main()
{
	int num =5;
	int fact;
	//call the funcation by passing number which
	//you want to get factorial
	fact = getFactorial(num);
	printf("Factorial of %d = %d",num,fact);

	return 0;
}

Factorial program in C using Recursion

This C code example calculate the factorial of a number using recursion technique. In brief, Recursion means, a function call itself again and again till it meets some specified condition.

Note that condition is necessary as if we don’t put the condition, the recursive call of function will go in infinite loop resulting memory over flow.

C Recursive code:

/* -------------------------------------------------------
* C Program to find factorial of a number using Recursion
*/
#include<stdio.h>
//Define a function that returns a factorial of a number

/*---------------------------------------
* Checking the condition, if number is 0 
* or 1 then it will return 1,
* otherwise it move to else block and call itself again.
*/
int getFactorial(int n){

	if (n == 0 || n == 1) {
		return 1;
	} else {
		return getFactorial(n - 1) * n;
	}
}

//Test factorial function 
int main()
{
	int num =5;
	int fact;
	//call the funcation by passing number which
	//you want to get factorial
	fact = getFactorial(num);
	printf("Factorial of %d = %d",num,fact);

	return 0;
}

NOTE:


In factorial program in C language, if you find a factorial of a large number, then you need to have data type of factorial variable “unsigned long” or “unsigned long long” as its factorial value will be very larger.

Related Posts