Write a C program to Find the Factorial of a Number.

Function Main
    Declare Integer n
    Declare Integer i
    Declare Integer fact
    Assign fact = 1
    Output "Enter an integer: "
    Input n
    If (n<0)
        Output "Error! Factorial of a negative number doesn't exist."
    Else
        For i = 1 to n
            Assign fact = fact * i
        End
        Output "Factorial is " &fact
    End
End

#include <stdio.h>
int main() {
    int n, i;
    int fact = 1;
    printf("Enter an integer: ");
    scanf("%d", &n);

    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");
    else {
        for (i = 1; i <= n; ++i) {
            fact *= i;
        }
        printf("Factorial is %d",fact);
    }

    return 0;
}

Enter an integer: 5
Factorial is 120 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.