Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Write a C program to get factorial of a number ?

Here’s a C program that takes an integer input from the user and prints the step-by-step factorial of that number:

#include <stdio.h>

int main() {
    int n, fact = 1;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    printf("Factorial of %d:\n", n);

    for (int i = 1; i <= n; i++) {
        fact *= i;
        printf("%d! = %d\n", i, fact);
    }

    return 0;
}

Output:

Enter a positive integer: 9
Factorial of 9:
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880

In this program, we first ask the user to enter a positive integer using the printf and scanf functions. We then use a for loop to calculate the factorial of that number step by step.

Inside the for loop, we multiply the current value of fact with the loop counter variable i, which ranges from 1 to n. We then print out the value of i and fact using the printf function. The ! symbol is used to denote the factorial operation.

Finally, the program returns 0 to indicate successful execution.