Factorial Program in C

Factorial Program in C

Factorial Program in C

Factorial Program in C: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:

5! = 5*4*3*2*1 = 120

3! = 3*2*1 = 6

Here, 5! is pronounced as “5 factorial”, it is also called “5 bang” or “5 shriek”.

The factorial is normally used in Combinations and Permutations (mathematics).

There are many ways to write the factorial program in c language. Let’s see the 2 ways to write the factorial program.

  • Factorial Program using loop
  • Factorial Program using recursion

Factorial Program using loop

Let’s see the factorial Program using loop.

#include<stdio.h>

int main()

{

int i,fact=1,number;

printf(“Enter a number: “);

scanf(“%d”,&number);

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

fact=fact*i;

}

printf(“Factorial of %d is: %d”,number,fact);

return 0;

}

Output:
Enter a number: 5
Factorial of 5 is: 120
Factorial Program using recursion in C

Let’s see the factorial program in c using recursion.

#include<stdio.h>

long factorial(int n)

{

if (n == 0)

return 1;

else

return(n * factorial(n-1));

}

 

void main()

{

int number;

long fact;

printf(“Enter a number: “);

scanf(“%d”, &number);

fact = factorial(number);

printf(“Factorial of %d is %ld\n”, number, fact);

return 0;

}

Output:
Enter a number: 6
Factorial of 5 is: 720

 

Related Post:

Features of C Programming Language

Variables in C

Data Types in C

Keywords in C

C Operators

Comments in C

Escape Sequence in C

C Functions

Storage Classes in C

Dynamic memory allocation in C

Leap year program in C

Fibonacci Series in C

Prime Number program in C

Palindrome program in C

Sum of digits program in C

Escape Sequence in C

ASCII value in C

Difference Between Type Casting and Type Conversion in C

Difference Between Variables and Constants

Matrix multiplication in C

C Program to generate Fibonacci Triangle

C Program to print “hello” without semicolon

C Program to swap two numbers without third variable

C Program to reverse number

Count the number of digits in C

Tokens in C

C Identifiers

C Strings

Compile time vs Runtime in C

C break statement

C goto statement

Type Casting in C

C String Functions

C Pointers

Dangling Pointers in C

void pointer in C

Pointer to Pointer in C

Recursion in C

Call by value and Call by reference in C

File Handling in C

C fprintf() and fscanf()

C fputc() and fgetc()

C fputs() and fgets()

C fseek() function

Constant Pointers in C

2 thoughts on “Factorial Program in C

  1. Pingback: Leap year program in C - CloudNClear

  2. Pingback: Pointer to Pointer in C - CloudNClear

Comments are closed.