Page

Program for multiplication of two matrices in java script

<html>
<head><h2><center>program for multiplication of two matrices"</h2></center></head>
<body>
<script type="text/javascript">
function mul()
{
m=parseInt(prompt("enter the number of rows for ist matrix"," "))
n=parseInt(prompt("enter the number of columns for ist matrix"," "));
p=parseInt(prompt("enter the number of rows for 2nd matrix"," "));
q=parseInt(prompt("enter the number of columns for 2nd matrix"," "));

if(n==p)
{
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(p);
for(i=0;i<p;i++)
{
b[i]= new Array(q);
}
alert("enter elements for second matrix");
for(s=0;s<p;s++)
{
for(t=0;t<q;t++)
{
b[s][t]=parseInt(prompt("enter value"," "));
}}

c=new Array(m);
for(i=0;i<m;i++)
{
c[i]=new Array(q);
}


for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<p;k++)
{
c[i][j]=parseInt(c[i][j]+parseInt((a[i][k]*b[k][j])));
}}}
document.write("the multiplication  of  two matrices is =>"+"<br>");
for(i=0;i<m;i++)
{
document.write("<br>");
for(j=0;j<q;j++)
{
document.write(c[i][j]+"&nbsp&nbsp");
}}}
else
document.write("multiplication not possible");
}
</script>
<button onclick="mul()">multiply</button>
</body>
</html>

No comments:

Post a Comment