Hi to all!!!! I Am Arpit Vijay Vergiya.
On this Blog i tried to share about Java.
I hope this blog will help you to learn many new things in java.
Keep visiting!!!
Keep sharing

by

Matrix multiplication


import java.util.Scanner;
class MatrixMul{
		public static void main(String args[])
		{
				int a[][]= new int[3][3];
				int b[][]= new int[3][3];
				int c[][]=new int[3][3];
				int i,j,k;
				
				Scanner sc = new Scanner(System.in);
				System.out.println("enter Element 9 of 1st String");
				for(i=0;i<3;i++)
				{
						for(j=0;j<3;j++)
						{
								a[i][j]=sc.nextInt();
						}
				}
				
				System.out.println("enter Element 9 of 2nd String");
				for(i=0;i<3;i++)
				{
						for(j=0;j<3;j++)
						{
								b[i][j]=sc.nextInt();
						}
				}
				
				System.out.println("Matrix A\tMatrix B");
				for(i=0;i<3;i++)
				{
						for(j=0;j<3;j++)
						{
								System.out.print(a[i][j]+" ");
						}
						
						System.out.print("\t\t");
						
						for(k=0;k<3;k++)
						{
								System.out.print(b[i][k]+" ");
						}
						System.out.println();
				}
				
				//logic for multiply
				
				int mul=0;
				for(i=0;i<3;i++)
				{
						for(j=0;j<3;j++)
						{
								
								for(k=0;k<3;k++)
								{
										mul+= a[i][j]*b[j][k];
								}
								
								c[i][j]=mul;
								mul=0;
						}
			
				}
				
				System.out.println("Result is:");
				for( i=0;i<3;i++)
				{
						for(j=0;j<3;j++)
						{
								System.out.print(c[i][j]+" ");
						}
						
						System.out.println(" ");
				}
				
		}
}

0 comments:

Post a Comment