Page 104 - PortGPT_V2.1_C8_Flipbook
P. 104
Preceding code will display the sum of first five natural numbers. Let us create a web page to use the
do-while loop.
Program 3: To display the sum of even and odd numbers on a web page using the do-while loop.
<HTML>
<HEAD>
<TITLE>
Using do-while Loop
</TITLE>
</HEAD>
<BODY>
<H3>
Using do-while Loop to Display Sum of Even and Odd Numbers upto 20 </H3>
<SCRIPT>
var evensum = 0;
var oddsum = 0;
var i = 0;
do
{
if(i%2==0)
{
evensum=evensum + i;
}
else
{
oddsum=oddsum + i;
}
i++;
}while(i<=20);
document.write("Sum of even numbers is: "+evensum + "<BR>");
document.write("Sum of odd numbers is: "+oddsum);
</SCRIPT>
</BODY>
</HTML>
Output of the Program 3 is given below:
102 Premium Edition-VIII

