codescracker
c

C Program to Find Factorial of Number



« Previous Program Next Program »

In this tutorial, we will learn about how to find and print factorial of any given number with and without using function. Here we have also created a recursive function to find out the factorial of any number. Here are the list of programs you will go through:

But before starting these programs, let's understand how factorial of a number gets calculated.

How to Find Factorial of any Number ?

To find factorial f of any number say n, use the formula

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

Or

n! = 1*2*3*....*(n-2)*(n-1)*n

For example, find factorial of 5, using the above formula

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

So factorial of 5 is 120. Now let's move on and implement it in a C program.

Find Factorial of Given Number

To find the factorial of any given number in C programming, you have to ask from user to enter the number. Now find the factorial of that number using above formula as shown in the program given below. As you can see from the formula, if a number is n, then multiplication gets occurred for n times. Or for example, if 5 is the number, then multiplication gets occurred 5 times, that is 5*4*3*2*1. So here it is required to create loop that runs n number of times

// Factorial of any Number in C
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, i, fact=1;
    printf("Enter any number: ");
    scanf("%d", &num);
    for(i=num; i>0; i--)
        fact = fact*i;
    printf("\nFactorial of %d = %d", num, fact);
    getch();
    return 0;
}

As the program was written under Code::Blocks IDE, therefore after successful build and run you will get the following output:

c program find factorial of number

Now supply any number say 5 and press ENTER key to see the factorial of 5 as output as shown here:

find factorial c

Program Explained

using Function

This program does the same job, but using user-defined function findFact(). To learn about function, then refer to its separate tutorial where you can go for its in-depth tutorial.

// Factorial of any Number in C using Function
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int findFact(int);
int main()
{
    int num, fact;
    printf("Enter any number: ");
    scanf("%d", &num);
    fact = findFact(num);
    printf("\nFactorial of %d = %d", num, fact);
    getch();
    return 0;
}
int findFact(int n)
{
    int i=n, f=1;
    while(i>0)
    {
        f = f*i;
        i--;
    }
    return f;
}

using Recursion

Here is another program that will calculate the factorial of any given number using recursive function:

// Factorial using Recursion in C
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int findFact(int);
int main()
{
    int num, fact;
    printf("Enter the number: ");
    scanf("%d", &num);
    fact = findFact(num);
    printf("Factorial = %d", fact);
    getch();
    return 0;
}
int findFact(int val)
{
    if(val==1)
        return val;
    else
        return val*findFact(val-1);
}

Here is the final snapshot of the sample run:

c factorial using recursive function

Program Explained

If you are not satisfied with above explanation. Then I recommend you to refer Recursion in C tutorial. There you will definetely understand all about it, that how recursion works.

Find Factorial of all Numbers in Given Range

Now this is the last program that will find factorial of all numbers in given range. For example, if user enters 1 and 10 as two number (the range). Then the program will find and prints factorial of all numbers from 1 to 10.

// Factorial of All number in Given Range
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int findFact(int);
int main()
{
    int num1, num2, i;
    long int fact;
    printf("Enter Range: ");
    scanf("%d %d", &num1, &num2);
    if(num1<num2)
    {
        for(i=num1; i<=num2; i++)
        {
            fact = findFact(i);
            printf("\nFactorial of %d = %ld", i, fact);
        }
    }
    else
    {
        for(i=num2; i<=num1; i++)
        {
            fact = findFact(i);
            printf("\nFactorial of %d = %ld", i, fact);
        }
    }
    getch();
    return 0;
}
int findFact(int n)
{
    int i=n;
    long int f=1;
    while(i>0)
    {
        f = f*i;
        i--;
    }
    return f;
}

Here is its sample run:

find factorial of all number in range c

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker