C Program for Factorial of a Number

Subject: PPS (Programming for Problem Solving)

Contributed By: Sanjay

Created At: February 3, 2025

Question:


Write a C Program for Factorial of a Number

Explanation Video:

Custom Image

Explanation:

 

Factorial of n is n! = n × (n-1) × (n-2) ... × 1.
We use a for loop to multiply numbers from 1 to n.

Source Code:
#include <stdio.h>

int main() {

   int num, i, fact = 1;

   printf("Enter a number: ");

   scanf("%d", &num);

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

       fact *= i;  // Multiply fact by i

   }

   printf("Factorial of %d is %d\n", num, fact);

   return 0;
}
Input:
Enter a number: 5
Output:
Factorial of 5 is 120
Share this Article & Support Us:
Status
printf('Loading...');