Page

Program to add two matrices of order m x n in java script

<html>
<head><h2><center>program to add two matrices of order m x n</h2></center></head>
<body>
<script type="text/javascript">
function add()
{
m=parseInt(prompt("enter the number of rows"," "));
n=parseInt(prompt("enter the number of columns"," "));
alert("enter elements for first matrix");
a = new Array(m);
for(i=0;i<m;i++)
{
a[i]=new Array(n);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=parseInt(prompt("enter value"," "));
}}
b=new Array(m);
for(i=0;i<m;i++)
{
b[i]=new Array(n);
}
alert("enter elements for second matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=parseInt(prompt("enter value"," "));
}}

c=new Array(m);
for(i=0;i<m;i++)
{
c[i]=new Array(n);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=parseInt(a[i][j]+b[i][j]);
}}
document.write("the addition of  two matrices is =>"+"<br>");
for(i=0;i<m;i++)
{
document.write("<br>");
for(j=0;j<n;j++)
{
document.write(c[i][j]+"&nbsp&nbsp");
}}
}
</script>
<button onclick="add()">sum</button>
</body>
</html>

No comments:

Post a Comment