Finding Factorial of a Number in C

CCBeginner
Practice Now

Introduction

In this lab, we will learn how to write a C program to find the factorial of a number. We will use a simple algorithm to calculate the factorial using a loop.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123240{{"`Finding Factorial of a Number in C`"}} c/comments -.-> lab-123240{{"`Finding Factorial of a Number in C`"}} c/variables -.-> lab-123240{{"`Finding Factorial of a Number in C`"}} c/data_types -.-> lab-123240{{"`Finding Factorial of a Number in C`"}} c/operators -.-> lab-123240{{"`Finding Factorial of a Number in C`"}} c/for_loop -.-> lab-123240{{"`Finding Factorial of a Number in C`"}} c/user_input -.-> lab-123240{{"`Finding Factorial of a Number in C`"}} c/memory_address -.-> lab-123240{{"`Finding Factorial of a Number in C`"}} c/pointers -.-> lab-123240{{"`Finding Factorial of a Number in C`"}} c/function_parameters -.-> lab-123240{{"`Finding Factorial of a Number in C`"}} c/function_declaration -.-> lab-123240{{"`Finding Factorial of a Number in C`"}} end

Understanding Factorial

Factorial (denoted by n!) for a number (say n) is the product of all the numbers before n with the number itself. We can say that,

!n = n * !(n - 1)

For example,

!6 = 6 * 5 * 4 * 3 * 2 * 1 = 720
!12 = 12 * 11 * 10 * 9 * 8 * 7 * !6 = 479,001,600

Important points:

  • Factorial of 0 is 1
  • Factorial of negative numbers does not exist.

Writing the Algorithm

Here are the steps to be followed for the factorial program:

  1. Declare variables n and fact=1. n is the number whose factorial is to be calculated and fact is the variable in which we will store the result.
  2. Read input from the user in n.
  3. Initialize loop iterator i=1 and run the loop till i<=n
  4. Do the following in each iteration of the loop :
    • fact=fact*i
    • i++
  5. Print fact.

Writing the C Program

In this program, we will use a for loop to find the factorial of a number.

#include<stdio.h>
int main()
{
    int n,i;
    long int fact=1;
    printf("Enter the number: ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        fact=fact*i;    // calculating factorial
    }
    printf("Factorial of %d is %ld",n,fact);
    return 0;
}

Testing the Program

Let's test our program by running it and checking the output.

Enter the number: 5
Factorial of 5 is 120

Final Program

Here is the final program that finds the factorial of a number using a for loop:

#include<stdio.h>
int main()
{
    int n,i;
    long int fact=1;
    printf("Enter the number: ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        fact=fact*i;    // calculating factorial
    }
    printf("Factorial of %d is %ld",n,fact);
    return 0;
}

Summary

In this lab, we learned how to write a C program to find the factorial of a number using a for loop. We used a simple algorithm and followed a step-by-step process to achieve our goal. The program takes an input from the user, calculates the factorial using a loop, and prints the result on the screen. We covered some important points like the initialization of variables and the limits of int and long int data types.

Other C Tutorials you may like