Wednesday, 18 September 2024

WRITE A JAVASCRIPT TO PRINT SUM OF GIVEN ODD OR EVEN NUMBERS


CODE:-

<html>

<head>

<title>Even Or Odd Sum</title>

<style type="text/css">

body{

background-color:yellow;

}

.tab{

text-transform:capitalize;

background-color:lightgrey;

}

.v2{

text-align:center;

}

.v1{

background-color:purple;

color:white;

text-transform:uppercase;

}

</style>

<script type="text/javascript">

function check()

{

var no=parseInt(document.f1.no.value);

var even=0,odd=0,sodd=0,seven=0;

for(var i=1;i<=no;i++)

{

if(i%2==0)

{

even++;

seven=seven+i;

}

else

{

odd++;

sodd=sodd+i;

}

}

document.f1.even.value=even;

document.f1.seven.value=seven;

document.f1.odd.value=odd;

document.f1.sodd.value=sodd;

return false;

}

</script>

</head>

<body>

<center>

<form name="f1" method="post" onsubmit="return check()">

<table border="1" class="tab">

<tr class="v1">

<th colspan="2"><h5>sum of even or odd number</h5></th>

</tr>

<tr>

<th>enter number:-</th>

<td><input type="text" name="no" size="5" style="border-radius:15px;"></td>

</tr>

<tr class="v2">

<td colspan="2">

<input type="submit" name="submit" value="Submit" style="border-radius:10px;background-color:lightgreen;border-color:orange;color:red;">

<input type="reset" name="reset" value="Reset" style="border-radius:10px;background-color:lightgreen;border-color:orange;color:red;">

</td>

</tr>

<tr>

<th>even numbers:-</th>

<td><input type="text" name="even" size="5" style="border-radius:15px;"></td>

</tr>

<tr>

<th>odd numbers:-</th>

<td><input type="text" name="odd" size="5" style="border-radius:15px;"></td>

</tr>

<tr>

<th>sum of even numbers:-</th>

<td><input type="text" name="seven" size="5" style="border-radius:15px;"></td>

</tr>

<tr>

<th>sum of odd numbers:-</th>

<td><input type="text" name="sodd" size="5" style="border-radius:15px;"></td>

</tr>

</table>

</form>

</center>

</body>

</html>


No comments:

Post a Comment