# Write a C program to find a factorial of a given number

Factorial of a given number is defined as the product of a given number with its previous number till that number reaches 1. In mathematical representation, factorial is represented by !

Factorial of 5! is

5*4*3*2*1 = 120

## Factorial of a number without using user defined function

#include int main() { int number = 5,i; long int factorial; factorial = 1; for(i = number; i >= 1; i--) { factorial = factorial * i; printf(" Factorial of %d is = %ld", number, factorial); return 0; }

## Output

Factorial of 5 is = 120

## Factorial of a number using user defined function

#include long int factorial(int n) { int i; long int factorial = 1; if(n==1) return factorial; for(i = n; i >= 1;i--) { factorial = factorial * i; return factorial; } } int main() { int number = 5; printf(" Factorial of %d is = %ld",number,factorial(number)); return 0; }

## Output

Factorial of 5 is = 120

## Factorial of a number using recursion

#include long int factorial(int n) { if(n==1) return 1; return n*factorial(n-1); } int main() { int number = 5; printf("Factorial of %d is = %ld",number,factorial(number)); return 0; }

## Output

Factorial of 5 is = 120

How was the tutorial? Nice. Right?

## Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.

## What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!## Recommended tutorials

- What is Palindrome number in C Language?
- Program for Fibonacci number in C Language
- String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions
- Last minute notes on Data Structures
- Write a C program to check whether a given number is prime or not
- Write a C program to check whether a given number is Armstrong number or not