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

Article by: Manish Methani

Last Updated: September 27, 2021 at 10:04am IST
1 min 32 sec

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

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>