Write a JavaScript code to find the sum of N natural Numbers. (Use user-defined function)

<html>
<head>
<script type = "text/javascript">
    var num = window.prompt("Enter the number:","");
    var n = parseInt(num);
    result = sumnaturalno(n);
    window.alert("The sum of " + n + "natural number is" + result);
    function sumnaturalno(n)
    {
        var i;
        var sum = 0;
        for(i = 1;i <= n; i++){
            sum = sum + i;}
        return (sum);
    }
</script>
</head>
</html>

9 comments:

  1. That was very helpful java code
    Thanks for sharing

    Get training on digital marketing training with our industrial experts in
    seo training in bangalore join today

    ReplyDelete
  2. Write a javascript code embeded in HTML that computes the sum of even numbers in the range 50 to

    100 and display the results on the screen

    ReplyDelete
  3. Write a HTML code embedded with a javascript function that will add the values 30 and 20 display the

    output

    ReplyDelete
  4. Write an ASP program embedded on HTML that would be used to display all the multiples of 5 in the

    range 1 and 50.

    ReplyDelete
  5. Write a HTML code embedded with a javascript function that will add the values 30 and 20 display the

    output

    ReplyDelete
  6. Using JavaScript, accept 5 numbers from the user & display the sum of all array elements

    ReplyDelete
  7. Write java script programme to calculate the sum of 1st 10 natural numbers

    ReplyDelete
  8. Write a JavaScript function to find sum of first 20 even natural numbers

    ReplyDelete