<html> <head> <script type="text/javascript"> <!-- function SumofNNaturalNumbers() { var N=10; var sum = 0; for(var i=1;i<=N;i++) sum+=i; alert("Sum Of N Natural Numbers : " + sum); } //--> </script> </head> <body> <input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" /> </body> </html>
-- Thanks & Regards, RNA Team
<html> <head> <script type="text/javascript"> <!-- function SumofNNaturalNumbers() { var N=10; var sum = 0; var i=1; while(i<=N) { sum+=i; i++; } alert("Sum Of N Natural Numbers : " + sum); } //--> </script> </head> <body> <input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" /> </body> </html>
<html> <head> <script type="text/javascript"> <!-- function SumofNNaturalNumbers() { var N=10; var sum = 0; sum = (N * (N+1)) /2; alert("Sum Of N Natural Numbers : " + sum); } //--> </script> </head> <body> <input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" /> </body> </html>
<html> <head> <script type="text/javascript"> <!-- function SumofNNaturalNumbers() { var N=10; var sum = 0; Array.apply(null,Array(N)) .map(function(_, index){ return index+1; }) .forEach(function(number) { sum += number; }); alert("Sum Of N Natural Numbers : " + sum); } //--> </script> </head> <body> <input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" /> </body> </html>
<html> <head> <script type="text/javascript"> <!-- function SumofNNaturalNumbers() { var N=20; var sum = 0; var sum = Array.apply(null,Array(N)) .map(function(_, index){ return index+1; }) .reduce(function(a,b){return a + b}) alert("Sum Of N Natural Numbers : " + sum); } //--> </script> </head> <body> <input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" /> </body> </html>
<html> <head> <script type="text/javascript"> <!-- function SumofNNaturalNumbers() { var N = 10; sum = SumofNNumbers(N); alert("Sum Of N Natural Numbers : " + sum); } //using recursive function function SumofNNumbers(n){ if(n==1) return n; else return (n + SumofNNumbers(n-1)); } //--> </script> </head> <body> <input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" /> </body> </html>
<html> <head> <script type="text/javascript"> <!-- function SumofNNaturalNumbers() { var N=10; var sum = 0; var i=1; do{ sum+=i; i++; }while(i<=N); alert("Sum Of N Natural Numbers : " + sum); } //--> </script> </head> <body> <input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" /> </body> </html>
<html> <head> <script type="text/javascript"> <!-- function SumofNNaturalNumbers() { var N=10; var sum = 0; var numbers = []; var count = 1; while ( count <= N+1 ) numbers.push( count++ ); for(var number in numbers) { sum += parseInt(number);} alert("Sum Of N Natural Numbers : " + sum); } //--> </script> </head> <body> <input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" /> </body> </html>
<html> <head> <script type="text/javascript"> <!-- function SumofNNaturalNumbers() { var N = 10; var sum = Array.apply(null,Array(N)) .map(function(_, index){ return index+1; }).reduce((a, b) => a + b); alert("Sum Of N Natural Numbers : " + sum); } //--> </script> </head> <body> <input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" /> </body> </html>
<html> <head> <script type="text/javascript"> <!-- function SumofNNaturalNumbers() { var N=10; var sum = 0; for(var i=N;i>=0;i--) sum+=i; alert("Sum Of N Natural Numbers : " + sum); } //--> </script> </head> <body> <input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" /> </body> </html>
<html> <head> <script type="text/javascript"> <!-- function SumofNNaturalNumbers() { var N=10; var sum = 0; while(N>0) { sum+=N; N--; } alert("Sum Of N Natural Numbers : " + sum); } //--> </script> </head> <body> <input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" /> </body> </html>
<html> <head> <script type="text/javascript"> <!-- function SumofNNaturalNumbers() { var N=10; var sum = 0; do{ sum+=N; N--; }while(N>0); alert("Sum Of N Natural Numbers : " + sum); } //--> </script> </head> <body> <input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" /> </body> </html>
Login to post response