C program to find factorial of a number

devquora
devquora

Posted On: Feb 29, 2024

C program to find factorial of a number

 

Overview:

In this article, First of all, I want to introduce you to Numbers. Then I will explain What is Factorial? Furthermore, I will demonstrate and write the logic for the factorial of a number. Moreover, I would like to explain How to find the factorial of a number? After all, I will show a C program to find the factorial of a number with output.

Table of contents:

  1. What is a Number?
  2. What is Factorial?
  3. Demonstration of factorial of a number
  4. How to find the Factorial of a number?
  5. C program to find the factorial of a number
  6. Conclusion

What is a Number?

A number is an object that uses digits to perform a mathematical task. Calculus that is a branch of mathematics included many numbers such as integers, whole numbers, real numbers, and imaginary numbers so on. Here, I will discuss numbers to perform arithmetic addition operations. Moreover, a Number is a combination of digits, some symbols, and decimal points. For instance, 127.23 is a rational number.

What is Factorial?

The product of an integer and all the integers below it is known as the factorial of that integer.

For example,

factorial four (4!) = 24.

The Factorial of a negative number doesn't exist.

Factorial is represented by '!', so five factorial is written as (5!), n factorial as (n!).

Also, n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one, i.e., 0! = 1.

Some other factorials examples are as follows:

0! = 1

1! = 1

2! = 1*2 =2

3! = 1*2*3 = 6

4! = 1*2*3*4 =24

5! = 1*2*3*4*5 = 120

The factorial of a positive number n is given by:

The factorial of n (n!) = 1 * 2 * 3 * 4....n

The factorial of a negative number n is not possible.

The factorial of 0 is always 1.

Demonstration of factorial of a number

Demo factorial

How to find the Factorial of a number?

  • Step 1: Take variables as n.
  • Step 2: Then, multiply each digit within the range from 1 to that variable.

C program to find the factorial of a number

#include 
int main() {
    int n, i;
    unsigned long long fact = 1;
    printf("Enter an integer: ");
    scanf("%d", &n);

    // shows error if the user enters a negative integer
    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");
    else {
        for (i = 1; i <= n; ++i) {
            fact *= i;
        }
        printf("The Factorial of %d = %llu", n, fact);
    }

    return 0;
}

The output of the Program:

The Factorial of 6 = 720

Demo factorial

Conclusion:

This Program store an integer in the int datatype. After the successful execution, the factorial of this number is shown as an output.

The factorial of a positive number n is given by:

The factorial of n (n!) = 1 * 2 * 3 * 4....n

In this program, we will find the factorial of a number where the number should be entered by the user. A Factorial is a sequence of numbers that multiply all previous numbers. 

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..