An XHTML program which reads a number ‘n’ and displays squares of 1 to ‘n’ numbers using Javascript

By | August 30, 2012

Input: A number n obtained using prompt

Output: A table of numbers from 1 to n and their squares using alert

 

 

<?xml version=”1.0″ encoding=”utf-8″?>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>

<title> square.html </title>

</head>

<body style = “background-color:yellow”>

<h3 style=”text-align:center;color:red”> Program to generate a table of numbers from 1 to n and

their squares using alert</h3>

<script type=”text/javascript”>

var n=prompt (“Enter the limit ‘n’ to generate the table of numbers from 1 to n:”,””);

var msg=””;

var res= “0”;

for(var x= 1; x<=n;x++)

{

res = x * x;

msg = msg + ” ” + x + ” * “+ x + ” = ” + res + “\n”;

}

alert(msg);

</script>

</body>

</html>

 

 

The output of this program is as follows

Please Share: Tweet about this on TwitterShare on FacebookShare on Google+Share on RedditPin on PinterestShare on LinkedInDigg thisShare on StumbleUponShare on TumblrBuffer this pageShare on VKEmail this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *