JavaScript for loop activity
JavaScript for loop activity
JavaScript for loop activity
Activity 1:
Activity 2:
<!DOCTYPE html>
<html>
<head>
<title>Activity</title>
</head>
<body>
<script type="text/javascript">
document.write('Find the 10 natural numbers:<br>');
document.write('----------------------<br>');
document.write('The natural numbers are:<br>');
var sum=0;
for(var a=1; a<=10; a++){
document.write(a+' ');
sum=sum+a;
}
document.write('<br> The sum of first 10 natural number: '+sum);
</script>
</body>
</html>
Activity 3:
<!DOCTYPE html>
<html>
<head>
<title>Activity3</title>
</head>
<body>
<script type="text/javascript">
var b=parseInt(prompt('Input a number of terms'));
document.write('Input a number of terms: '+b);
document.write('<br>The natural numbers upto '+b+'th terms are:<br>');
var sum=0;
for(var a=1; a<=b; a++){
document.write(a+' ');
sum=sum+a;
}
document.write('<br> The sum of the natural numbers is: '+sum);
</script>
</body>
</html>
Activity 4:
<!DOCTYPE html>
<html>
<head>
<title>Activity4</title>
</head>
<body>
<script type="text/javascript">
var m = (prompt('Write pattern character'));
var k = parseInt(prompt("Input the characters for a side"));
document.write('Print a pattern like square with '+m+' character: <br>');
document.write('---------------------------------------<br>')
document.write('Input the number of characters for a side: '+k+'<br>')
for(var i=1; i<=k; i++){
for(var j=1; j<=k; j++){
document.write(' '+m+' ');
}
document.write('<br>');
}
</script>
</body>
</html>
Activity 5:
<!DOCTYPE html>
<html>
<head>
<title>Activity5</title>
</head>
<body>
<script type="text/javascript">
var b=0;
var n = parseInt(prompt('Input the number upto'));
document.write('Input the number upto: '+n);
document.write('<br>Multiplication table from 1 to '+n+'<br>');
for(i=1; i<=10; i++){
for(a=1; a<=n; a++){
b=a*i;
document.write(a+' * '+i+' = '+b+' ');
}
document.write('<br>');
}
</script>
</body>
</html>
Activity 6:
<!DOCTYPE html>
<html>
<head>
<title>Activity6</title>
</head>
<body>
<script type="text/javascript">
var sum=0;
var n = parseInt(prompt('Input number of terms'));
document.write('Input number of terms: '+n);
document.write('<br>The odd numbers are: ');
for(var i=1; i<=2*n; i++){
if(i%2){
document.write(i+' ');
sum=sum+i;
}
}
document.write('<br> The Sum of odd Natural Numbers upto '+n+' terms: '+sum);
</script>
</body>
</html>
Activity 7:
<!DOCTYPE html>
<html>
<head>
<title>Activity7</title>
</head>
<body>
<script type="text/javascript">
var sum=0;
var n = parseInt(prompt('Input the number of terms'));
document.write('Input number of terms: '+n);
document.write('<br>The even numbers are: ');
for(var i=1; i<=2*n; i++){
if(i%2 == 0){
document.write(i+' ');
sum=sum+i;
}
}
document.write('<br> The Sum of even Natural Numbers upto '+n+' terms: '+sum);
</script>
</body>
</html>
No comments:
Post a Comment