C Program to Find Factorial of a Number

In this post, you will learn how to find the factorial of a number using C Programming language.

C Program to Find Factorial of a Number

The factorial of a non-negative integer n, denoted by n! is the product of all positive integers less than or equal to n.

Factorial of n (n!) = 1  * 2 * 3 . . . n

The factorial of 0 (0!) is 1 and the factorial of negative numbers doesn’t exist.

So, without further ado, let’s begin the tutorial.

C Program to Find Factorial of a Number

// C Program To Find Factorial of a Number
#include <stdio.h>
int main(){
    int num, i;
    long int factorial = 1;
    
    // Asking for Input
    printf("Enter an Integer: ");
    scanf("%d", &num);
    
    // logic
    if (num < 0){
        printf("Factorial of Negative Numbers Doesn't Exist.");
    }
    else {
        for (i = 1; i <= num; ++i){
            factorial = factorial * i;
        }
        printf("The Factorial of %d is %ld.", num, factorial);
    }
    return 0;
}

Output 1

Enter an Integer: 7
The Factorial of 7 is 5040.

Output 2

Enter an Integer: 0
The Factorial of 0 is 1.

Output 3

Enter an Integer: -5
Factorial of Negative Numbers Doesn't Exist.

How Does This Program Work ?

    int num, i;
    long int factorial = 1;

In this program, we have declared two int type variables named num and i. We have also declared a long int variable to store the factorial value because factorial contains large numbers.

     // Asking for Input
    printf("Enter an Integer: ");
    scanf("%d", &num);

Then, the user is asked to enter an integer. The value of this integer will get stored in the num variable.

    // logic
    if (num < 0){
        printf("Factorial of Negative Numbers Doesn't Exist.");
    }

Now, first we check the integer, whether it is greater than 0 or less than 0. If it is less than 0, then our program will display “Factorial of Negative Numbers Doesn’t Exist.”

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

And if the integer is greater than 0, then the following logic will be used to find the factorial.

        printf("The Factorial of %d is %ld.", num, factorial);

printf() function will be used to display the output. %ld is a long int format specifier.

Some of the used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program. 

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively. 

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.

scanf() – scanf() function is used to take input from the user.  

printf() – printf() function is used to display and print the string under the quotation to the screen. 

For loop – A loop is used for initializing a block of statements repeatedly until a given condition returns false.

If. . . else – An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True. 

// – Used for Commenting in C. 

Conclusion

I hope after going through this post, you learned how to find the factorial of a number.

If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *