Javascript to display square of n numbers

By | January 30, 2016

This post explains a Javascript to display square of 1 to n numbers.

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

Input to Javascript to display square of n numbers

output of Javascript to display square of n numbers





Author: Ravi Bandakkanavar

A Techie, Blogger, Web Designer, Programmer by passion who aspires to learn new Technologies every day. It has been 6 years since I have been publishing articles and enjoying every bit of it. I want to share knowledge and build a great community with people like you.

Suggested read for you:

Did it help? Comment here..