46. JavaScript Program to Compute the 1+4+8+11+ ……..+n Series.
How does this program work?
- This program is used to print Number Triangle using JavaScript.
Here is the code
<html>
<head>
<title>JavaScript program to Find the Sum of given series</title>
</head>
<body>
<table>
<tr>
<td> <input type="text" name="a" id="first" placeholder="Enter any number"> </td>
</tr>
<tr>
<td> <button onclick="series4()" >Submit</button> </td>
</tr>
</table>
</body>
<script type="text/javascript">
function series4()
{
var n,i,num= 1,sum=0;
n = parseInt(document.getElementById ("first").value);
document.write( "Series till the enter number:</br>");
document.write("</br>");
for(i = 1; i <= n; i++)
{
if(i==1)
{
num = 1;
}
else
if(i%2 == 0)
{
num = num+3;
}
else
{
num = num + 4;
}
sum = sum+num;
document.write( num+ " ");
}
document.write( "</br>Sum of the series : "+sum);
}
</script>
</html>