C program to calculate the factorial of the given number

We can make our program to calculate the factorial of the given number efficiently. First, we all know that Factorial of a number is a product of all the integers below that number. Calculating a factorial by using C program is of course very easy as a single program will give you the result of factorial of any number. The process of computation is efficient and less time-consuming. In the following code, we assume that we are going to calculate the factorial of the number ‘n’. Here we make the use of For loop to generate all the numbers starting from ‘1’ to ‘n’. All these series of numbers will multiply the given number. The final product thus calculated will be the factorial of the number. Following code illustrate the technique to calculate the factorial of any number.

//To calculate the factorial of the given number
#include<stdio.h>
int main(){
	int n, i, fact=1;
	printf("Enter the given number: ");
	scanf("%d", &n);
	for(i=1;i<=n;i++)
	{
		fact = fact*i;
	}
	printf("factorial of %d is: %d", n, fact);
}

The above code will display the output as below:
Output:

Enter the given number: 6
factorial of 6 is: 720

 

SHARE C program to calculate the factorial of the given number

You may also like...

Leave a Reply

Your email address will not be published.

Share