 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to explain how you can calculate factorial in P program. So first I'm going to explain you how mathematically we calculate factorial. Then we will implement a program in C so that you can understand how that will be implemented. So, for example, we have a number whose value is 5. Now we need to calculate factorial of 5. So we calculate factorial as per this movie. You start from 1 and till that actual number you need to multiply all the natural numbers. So for our example, we took a number 5. So we are starting from 1 and we are multiplying all the digits between 1 to 5. And whatever will be the result of this calculation that will be the factorial. Now, you can also implement this in this way. So I am going to tell you two loops. One will be calculating factorial in increment order and second will be calculating factorial in decreasing order. So now I'm going to write a C program. Then I will iterate the loop cycle with this example so that you will be able to understand how a program is implemented and how we implement it. So I'm starting from the main function. Inside main, I am going to declare three variables. So n will be having the input value for that we need to calculate the factorial. i will be going to use to iterate the loops and f that is initialized with one will be having the calculated factorial. And then I mentioned the semicolon here. So after this variable declaration, I am using printf and this printer will display the message enter a number on screen. Then using scanf, I am going to receive the number that will be stored inside the n variable. So after reading the number from a user into n variable, now we can apply the loop. So to implement loop, we need three things. Initial value, second is condition and third is increment or decrement. So these are three parts. Those are required to implement the loop. So I'm going to implement this problem with the help of for loop. So let's identify all three parts of the loop then we can easily implement the loop. So initial value means beginning value. So beginning value we can identify as I equals to one. So I will begin the loop from one. So I'm initializing I value with one. Now, what will be the termination condition of the loop? So it depends upon the end. So if n is five, then loop will repeat five times if n is 10, loop will repeat 10 times if n is 15, loop will repeat 15 times. So here you can identify the condition as I less than equals to n. You can write it like as I less than equals to n. So according to this condition, loop will start from one and it will iterate till the value which is available inside n. Now starting from one, we need to move to two, then three, then four and up to n. So to apply this, we need this operation. I equals to I plus one and in C, we can implement it like this. I plus plus. Now for this decreasing part, I will change the situation and then accordingly loop will be implemented. So first let's implement the loop for increasing order, then easily we will be converting that into decreasing order. So now you can put all the values. So this is initialization of the loop that I equals to one. Then I am putting condition I less than equals to n and I am implementing the loop with I plus one. Now inside loop I am writing F equals to F into I. And after completion of loop, with the help of print F, I am going to display factorial that is stored inside F. So this way I implemented this program. Now I am going to tell you how this calculation is performing. So let's say initial value of I is one, value of n is five and we initialize F with one. Now I will tell you first why F is initialized with one, not with zero. Because if we use any variable for multiplication purpose and if we start its value with zero, then the outcome of this calculation will be zero. So in first notation I is one, then we need to check this condition. So one less than equals to and n is five. So one is less than equals to five, it means condition is two. Then we have to move to this situation or this statement. So here you can see F into I is written. So current value of F is one, current value of I is one. So one into one, new value of calculation will be stored inside F. So F will remain one right now. Now we will move to I plus plus. So I plus plus will change the value of I to two. So its new value is now two. Then again we need to check the condition. So I is two and n is five. So two less than equals to five condition is two right now. Now we come here. So current value of F is one, current value of I is two. So here one into two will be calculated and the calculated result will be stored inside F. So new value of F is now two. Then again I plus this will take place. So new value of I is three, then check the condition. It is true. Then we need to calculate this. So current value of F is two, current value of I is three. So three into two, six. So new value of F will become six. So this way you need to iterate this loop. Then I plus plus now value of I is four. So again check the condition. It is true because I less than equals to five, which is true. Current value of F six, current value of I is four. So six into four is 24. So that will be assigned into F because of this statement. And again, we need to go to I plus plus. So I will become five. Then this condition will be checked. So five less than equals to five, which is again true. Then this will be calculated. So current value of F is 24. I is five. So 24 into five is 120. That will be assigned into F. Now again, I will be implemented. So it's new value will be six. Six less than equals to five. So it is false. It means here loop will be terminated and this printed will print this factorial equals to, and this person will be replaced with the value of F. So the outcome will be factorial equals to 120. So this outcome will be displayed on the output screen. So this way we calculated factorial with N40 programming. So I explained this logic that is going, that is going in incremented order. Now, if you want to implement this method, starting from N and you need to move to one. So what you need to do, you need to change these three things. So I equals to one in place of this, you can put I equals to N. Now condition will be I greater than equals to one. So it will iterate till this condition is true. If it is false, it will be terminated. And instead of I plus plus, we need to use I minus minus. Now I'm going to tell you one rotation so that you can understand this. So initially I is five because we are assigning N. N is five and F is initially one because of this, right? Now let's iterate it. So first I equals to N five. We have to check the condition. So five greater than equals to one, it means condition is true. Now we will move here. So F is one and I is five. So five into one equals to five. So this time initial value or you can say first rotations value or calculated value of F will be five. Then I minus minus will take this. So I will become four. Then we need to check this condition for greater than equals to one. So it is true. Then again multiplied is so current value of F is five and current value of five is four. So five into four is 20. So this way this calculation will go on. And after five predation 120 will be stored inside F. And after completion of this loop, you can print that on output screen with the help of this printer. So I hope this way you understood how we can calculate factorial with the help of C program. I explained you both the ways like how you can implement the loop and increasing order and how you can implement this in decreasing order. So I hope you understood whatever I explained go on my channel and search for the playlist. So there are various playlist available. You can watch them and learn programming because programming is very easy to understand. So I hope whatever I explained in this video you understood. So keep watching videos and keep following my YouTube channel. Thank you for watching this video.