Skip to main content

C Program to Find Factorial of a Number

C Program to Find Factorial of a Number

The factorial of a positive integer n is equal to 1*2*3*...n. You will learn to calculate the factorial of a number using for loop in this example.
Factorial of a positive number
To understand this example, you should have the knowledge of following C programming topics:
The factorial of a positive number n is given by:
factorial of n (n!) = 1*2*3*4....n
The factorial of a negative number doesn't exist. And, the factorial of 0 is 1, 0! = 1

Example: Factorial of a Number

#include <stdio.h>
int main()
{
    int n, i;
    unsigned long long factorial = 1;

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

    // show 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)
        {
            factorial *= i;              // factorial = factorial*i;
        }
        printf("Factorial of %d = %llu", n, factorial);
    }

    return 0;
}
Output
Enter an integer: 10
Factorial of 10 = 3628800
This program takes a positive integer from the user and computes factorial using for loop.
Since the factorial of a number may be very large, the type of factorial variable is declared as unsigned long long.
If the user enters negative number, the program displays error message.
You can also find the factorial of a number using recursion.

Comments

Popular posts from this blog

C Program to Check Whether a Number is Positive or Negative

C Program to Check Whether a Number is Positive or Negative In this example, you will learn to check whether a number (entered by the user) is negative or positive.    To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C if...else Statement This program takes a number from the user and checks whether that number is either positive or negative or zero. Example #1: Check if a Number is Positive or Negative Using if...else #include <stdio.h> int main () { double number ; printf ( "Enter a number: " ); scanf ( "%lf" , & number ); if ( number <= 0.0 ) { if ( number == 0.0 ) printf ( "You entered 0." ); else printf ( "You entered a negative number." ); } else printf ( "You entered a positive num

C Program to Multiply two Floating Point Numbers

C Program to Multiply two Floating Point Numbers In this program, user is asked to enter two numbers (floating point numbers). Then, the product of those two numbers is stored in a variable and displayed on the screen.   To understand this example, you should have the knowledge of following C programming topics: C Programming Constants and Variables C Programming Data Types C Input Output (I/O) C Programming Operators Program to Multiply Two Numbers #include <stdio.h> int main () { double firstNumber , secondNumber , product ; printf ( "Enter two numbers: " ); // Stores two floating point numbers in variable firstNumber and secondNumber respectively scanf ( "%lf %lf" , & firstNumber , & secondNumber ); // Performs multiplication and stores the result in variable productOfTwoNumbers product = firstNumber * secondNumber ; //

C Program to Check Whether a Number is Palindrome or Not

C Program to Check Whether a Number is Palindrome or Not This program reverses an integer (entered by the user) using while loop. Then, if statement is used to check whether the reversed number is equal to the original number or not.   To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C if...else Statement C Programming while and do...while Loop An integer is a palindrome if the reverse of that number is equal to the original number. Example: Program to Check Palindrome #include <stdio.h> int main () { int n , reversedInteger = 0 , remainder , originalInteger ; printf ( "Enter an integer: " ); scanf ( "%d" , & n ); originalInteger = n ; // reversed integer is stored in variable while ( n != 0 ) { remainder = n % 10 ; reversedInteger = reversed