Simple Factorial Program In C

Here’s a simple factorial program in C:

#include <stdio.h>

int main()
{
    int num, i, fact=1;

    printf("Enter a number: ");
    scanf("%d", &num);

    for(i=1; i<=num; i++)
    {
        fact = fact * i;
    }

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

    return 0;
}

In this program, we first declare the variables num, i, and fact, where num is the number for which we want to find the factorial, i is a loop variable, and fact is the variable that will hold the factorial value.

We then prompt the user to enter a number using the printf() function and read the input using the scanf() function.

Next, we use a for loop to calculate the factorial of the entered number. The loop runs from 1 to num and multiplies the value of fact by the loop variable i in each iteration. This results in the factorial of num being stored in fact at the end of the loop.

Finally, we print the factorial of the entered number using the printf() function.

Note that this program assumes that the entered number is a positive integer. If a negative number or a decimal value is entered, the program may not work correctly.

Posted in: C