JavaScript program to find the sum of natural numbers using recursion:

Created with Sketch.

JavaScript program to find the sum of natural numbers using recursion:

function sumOfNaturalNumbers(number) {
  if (number === 1) {
    return 1;
  } else {
    return number + sumOfNaturalNumbers(number - 1);
  }
}

let number = parseInt(prompt("Enter a number:"));
console.log("The sum of natural numbers upto " + number + " is: " + sumOfNaturalNumbers(number));

Leave a Reply

Your email address will not be published. Required fields are marked *