Know the logic : Factorial of a number using C

Find Factorial of a number using C

In mathematics if we have been asked to find the factorial of any number then, we will start multiplying that number to it’s previous numbers until we reach 1.

Factorial is represent using “!” sign

Example : If we have been ask to find factorial of number 5 then, we what we will do is this..

5 x 4 x 3 x 2 x 1 = 120

120 is factorial of 5.

ITVoyagers

In mathematics, logic for finding factorial is quite straightforward but when it comes to translating this logic in programming language like C, then we have to write few lines of code to find the factorial of a number.

Following is one of the basic program to find the factorial of a number.

#include<stdio.h>                                                           // Include stdio header file for input output operations

 void main(){
     int num, i, temp=1;                                                    // Int variable declerations

     printf(“Enter any number : “);
     scanf(“%d”,&num);                                                     // Accept number from user

     if(num==0){                                                                 // Check if the received number zero
     printf(“Factorial of 0 is 1”);                                       
     }
     else if(num>0){                                                           // Check if accepted number is greater then 0
         for(i=num; i>0; i——){                                          // To multiply number with all the number below it
             temp*=i;                                                               // Example accepted number is 4 then factorial is 4 * 3 * 2 * 1 =24
         }
         printf(“Factorial of %d is %d”,num,temp);          // To print factorial of accepted number
     }
     else{                                                                              // If accepted number is negative then this block will get executed
         printf(“Please enter non-negative number”);
     }
 }

Line by line explanation for above factorial program

First we have to add header file. Now question arises “What is header file?” and “Why we have to use them?”.

Allow us to explain it. Let’s take first question

What is header file? and Why we use them?

  • Most of the time we use builtin functions like printf(), scanf(), etc. But never wrote any definition for any of the builtin functions or macros.
  • Because these functions are defined in header files, and if we want to use these functions then we have to include respective header file.
  • stdio stands for “stander input output” this header file has all the input output operations like printf(), scanf().

To include header files in our program we have to use “#include” followed by name of the header file in angle brackets “<” and “>

When we execute C program compiler search for main() function.

We have added main() function which will help compiler to execute the program. We have kept void  as a return type of the main() function which indicates that function will return nothing.

Now to find factorial we declared 3 variables of int data type and set value of temp variable to 1.

Next we have print message “Enter any number : ” with the help of printf() function which is use to print values on screen.

printf(“Enter any number : “) 

To accepts a number in num variable, we have used scanf() function which is use to accept values from user. “%d” represents that it is going to accept digit or integer value.

scanf(“%d”,num)

Next we have used if Block which will get executed if accepted number is 0 or not and if it is 0 then print “Factorial of 0 is 1”.

if(num==0)

{

    printf(“Factorial of 0 is 1”);

}

Next we have used else if block which will get executed if accepted number is greater then 0. If accepted number is greater then 0 then program will shift to for loop for execution.

else if(num>0)

{

for(i=num; i>0; i——)

{

temp*=i;

}

printf(“Factorial of %d is %d”,num,temp);

}

for loop well set “num’s” value to “i” and it will reduce value of “i” by 1 and it iterate the block of code till value of “i” reaches 1.

for(i=num; i>0; i——)

{

temp*=i;

}

Now there is a single line pf code in for loop block which is..

temp*=i

Above line will execute till the for loop condition is true. What for loop we do is it will multiply the accepted number with all the number below it.

let’s assume entered number is 4. Factorial of 4 is 24.

value of num = 4 which is initialized to i so value of i = 4. And for loop condition is true because i>0 hence it will start iteration.

#iteration 1

i = 24 and temp = 1(we have already set temp to 1)

temp*=i means temp = temp * i

which means new value of is temp = 1 * 4

after first iteration value of temp = 4.

#iteration 2

i = 3 and temp =4

New value of temp = temp * i i.e. temp = 4 * 3

New value of temp = 12

#iteration 3

i = 2 and temp =12

New value of temp = temp * i i.e. temp = 12 * 2

New value of temp = 24

#iteration 4

i = 1 and temp =24

New value of temp = temp * i i.e. temp = 24 * 1

New value of temp = 24

now the value of i will reduced to 0 which will break the for loop condition. hence there will be no further iterations.

Then we will print the factorial of the accepted number with the help of following code.

printf(“Factorial of %d is %d”,num,temp);

Now if user enters any negative number program will shift to else block. In this we are printing message that user must enter non-negative number

else

{

print(“Please enter non-negative number”);

}

Hope your logic is cleared through this explanation.

This blog aims at delivering knowledge to ensure uninterrupted learning experience.

Do checkout other related programs

Leave a Comment