logo

NJP

get the Factorial of a number using Javascript

Import · May 04, 2021 · video

welcome back guys in this video i'm going to demonstrate how we can calculate the factorial of a number using javascript if you are not already aware what factorial of a number is then let's take an example if i talk about number 3 then factorial of number three is going to be three into two into one and if i talk about factorial of number five then it is going to be five into four into three into two into one which equals to one twenty so let me show you the code for that and how it works so i'm going to create a function named fact and i'm passing a variable name num num is the number for which i want to calculate the factorial and i'm creating a output variable as well which stores the value of the final output and i'm initializing it with a value of one and then a loop which i did through the number and gives me the final result at the end of the loop to include the number itself i need to use less than equal to then i'm going to use the increment operator to increase the count by one then here i'm simply multiplying [Music] the output variable with the count variable fact equals to fact into count and in the end it's pretty straightforward i can simply return the fact variable which contains the output and here i am calling my function and i'm passing a visible so if i've got if i have to calculate the factorial of number three then the value is six three into two into one six let's calculate it for number five should be 120 if the code is working right it is 120 indeed you can also calculate for other numbers as well so thank you for watching

View original source

https://www.youtube.com/watch?v=Vfy2fOE93Xg