for loops js practice sum , factorial

Sum of all number


 let sum = 0;

let n = 3;

for(let i = 0; i < n; i++){

  sum += (i+1)

}


console.log(`the sum of first ${n} natural number is ${sum}`)


-----------------------------------------------------------------------------------------------------------------


let num = 5;

let factorial = 1;

for(let i = 1; i <= num; i++){

  factorial *=i;

}

console.log(`the factorial of {num} is : ${factorial}`)


---------------------------------------------------------------------------------------------------------------------


Comments

Popular posts from this blog

practice test of Arithmetic Operations: