Day 88: Sum of Numbers Using Recursion of 100DaysCodingChallenge

Hello all, 

#day88 

Day88 

Today's task was to Write a Program to Find Sum of N Natural Numbers (entered by the user)
using Recursion.

I have started the 100 days of coding challenge to improve my programming skills..

Code : 

// program to find the sum of natural numbers using recursion
function sum(num) {
  if (num > 0) {
    return num + sum(num - 1);
  }
  else {
    return num;
  }
}
// take input from the user
const number = parseInt(prompt('Enter a Positive Number : '));
const result = sum(number);
// display the result
console.log(`The Sum is ${result}`);

Output :     

 



Happy Coding:)

Thank you...      

Comments

Popular posts from this blog

Day 55: Pattern using loops of 100DaysCodingChallenge

Day 7: Square or Power Integer Numbers of 100DaysCodingChallenge

Day 45: Pattern using loops of 100DaysCodingChallenge