Multiplication of two matrix program

Matrix multiplication in c

#include<stdio.h>
#include<conio.h>
void MultiplicationTwoMatrix();
void MultiplicationTwoMatrix()
{
	int rows;
	printf("How many rows do you want to use the first matrix \n");
	scanf("%d",&rows);
	int columns;
	printf("How many columns do you want to use the first matrix \n");
	scanf("%d",&columns);
	int firstMatrix[10][10];
	printf("Enter your first matrix \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			scanf("%d",&firstMatrix[i][j]);
		}
	}
	int secondMatrix[10][10],a,b;
	printf("How many rows do you want to use the second matrix \n");
	scanf("%d",&a);
	printf("How many columns do you want to use the second matrix \n");
	scanf("%d",&b);
	if(columns!=a)
	{
		printf("Matrix multiplication is not possible \n");
	}
	else
	{

	printf("Enter your second matrix \n");
	for(int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			scanf("%d",&secondMatrix[i][j]);
		}
	}
	int sum=0,multiply[10][10];
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<b;j++)
		{
			for(int k=0;k<a;k++)
			{
				sum+=firstMatrix[i][k]*secondMatrix[k][j];
			}
			multiply[i][j]=sum;
			sum=0;
		}
	}
	printf("Multiplication of matrix = \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<b;j++)
		{
			printf("%d\t",multiply[i][j]);
		}
		printf("\n");
	}
}
}
void main()
{
	MultiplicationTwoMatrix();
	getch();
}

Output

How many rows do you want to use the first matrix 2 How many columns do you want to use the first matrix 2 Enter your first matrix 1 2 3 4 How many rows do you want to use the second matrix 2 How many columns do you want to use the second matrix 2 Enter your second matrix 5 6 0 7 Multiplication of matrix = 5 20 15 46

Matrix multiplication in c++

#include<iostream>
#include<conio.h>
using namespace std;
class MatrixMultiplication{
public:
void MultiplicationTwoMatrix()
{
	int rows;
	cout<<"How many rows do you want to use the first matrix \n";
	cin>>rows;
	int columns;
	cout<<"How many columns do you want to use the first matrix \n";
	cin>>columns;
	int firstMatrix[10][10];
	cout<<"Enter your first matrix \n";
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			cin>>firstMatrix[i][j];
		}
	}
	int secondMatrix[10][10],a,b;
	cout<<"How many rows do you want to use the second matrix \n";
	cin>>a;
	cout<<"How many columns do you want to use the second matrix \n";
	cin>>b;
	if(columns!=a)
	{
		cout<<"Matrix multiplication is not possible \n";
	}
	else
	{

	cout<<"Enter your second matrix \n";
	for(int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			cin>>secondMatrix[i][j];
		}
	}
	int sum=0,multiply[10][10];
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<b;j++)
		{
			for(int k=0;k<a;k++)
			{
				sum+=firstMatrix[i][k]*secondMatrix[k][j];
			}
			multiply[i][j]=sum;
			sum=0;
		}
	}
	cout<<"Multiplication of matrix = \n";
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<b;j++)
		{
			cout<<multiply[i][j]<<"  ";
		}
		cout<<"\n";
	}
}
}
};
void main()
{
	MatrixMultiplication ma;
	ma.MultiplicationTwoMatrix();
	getch();
}

Output

How many rows do you want to use the first matrix
2
How many columns do you want to use the first matrix
2
Enter your first matrix
1 2
2 4
How many rows do you want to use the second matrix
2
How many columns do you want to use the second matrix
2
Enter your second matrix
3 4
1 2
Multiplication of matrix =
5  8
10  16

Matrix multiplication in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MatrixMultiplicationProject
{
    class MatrixMultiplication
    {
public void MultiplicationTwoMatrix()
{
	int rows;
	Console.Write("How many rows do you want to use the first matrix \n");
	rows=Convert.ToInt32(Console.ReadLine());
    int columns;
	Console.Write("How many columns do you want to use the first matrix \n");
	columns=Convert.ToInt32(Console.ReadLine());
	int[,] firstMatrix=new int[10,10];
	Console.Write("Enter your first matrix \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			firstMatrix[i,j]=Convert.ToInt32(Console.ReadLine());
		}
	}
	int[,] secondMatrix=new int[10,10];int a,b;
	Console.Write("How many rows do you want to use the second matrix \n");
	a=Convert.ToInt32(Console.ReadLine());
    Console.Write("How many columns do you want to use the second matrix \n");
	b=Convert.ToInt32(Console.ReadLine());
	if(columns!=a)
	{
		Console.Write("Matrix multiplication is not possible \n");
	}
	else
	{

	Console.Write("Enter your second matrix \n");
	for(int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			secondMatrix[i,j]=Convert.ToInt32(Console.ReadLine());
		}
	}
	int sum=0;int [,] multiply=new int[10,10];
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<b;j++)
		{
			for(int k=0;k<a;k++)
			{
				sum+=firstMatrix[i,k]*secondMatrix[k,j];
			}
			multiply[i,j]=sum;
			sum=0;
		}
	}
    Console.Write("Multiplication of matrix = \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<b;j++)
		{
            Console.Write(multiply[i,j]+"  ");
		}
        Console.Write("\n");
	}
}
}
static void Main(string[] args)
{
    MatrixMultiplication mm = new MatrixMultiplication();
    mm.MultiplicationTwoMatrix();
    Console.ReadLine();
}
    }
}

Output

How many rows do you want to use the first matrix
2
How many columns do you want to use the first matrix
2
Enter your first matrix
1 2
3 4
How many rows do you want to use the second matrix
2
How many columns do you want to use the second matrix
2
Enter your second matrix
5 6
0 7
Multiplication of matrix =
5       20
15      46

Matrix multiplication in java

import java.util.*;
public class Main
{
public static void MultiplicationTwoMatrix()
{
    Scanner sc=new Scanner(System.in);
	int rows;
	System.out.print("How many rows do you want to use the first matrix \n");
	rows=sc.nextInt();
    int columns;
	System.out.print("How many columns do you want to use the first matrix \n");
	columns=sc.nextInt();
	int[][] firstMatrix=new int[10][10];
	System.out.print("Enter your first matrix \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			firstMatrix[i][j]=sc.nextInt();
		}
	}
	int[][] secondMatrix=new int[10][10];int a,b;
	System.out.print("How many rows do you want to use the second matrix \n");
	a=sc.nextInt();
    System.out.print("How many columns do you want to use the second matrix \n");
	b=sc.nextInt();
	if(columns!=a)
	{
		System.out.print("Matrix multiplication is not possible \n");
	}
	else
	{

	System.out.print("Enter your second matrix \n");
	for(int i=0;i<a;i++)
	{
		for(int j=0;j<b;j++)
		{
			secondMatrix[i][j]=sc.nextInt();
		}
	}
	int sum=0;int [][] multiply=new int[10][10];
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<b;j++)
		{
			for(int k=0;k<a;k++)
			{
				sum+=firstMatrix[i][k]*secondMatrix[k][j];
			}
			multiply[i][j]=sum;
			sum=0;
		}
	}
    System.out.print("Multiplication of matrix = \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<b;j++)
		{
            System.out.print(multiply[i][j]+"  ");
		}
        System.out.print("\n");
	}
}
}
	public static void main(String[] args) {
		MultiplicationTwoMatrix();
	}
}

Output

How many rows do you want to use the first matrix 
2
How many columns do you want to use the first matrix 
2
Enter your first matrix 
1 2
3 4
How many rows do you want to use the second matrix 
2
How many columns do you want to use the second matrix 
2
Enter your second matrix 
5 6
0 7
Multiplication of matrix = 
5  20  
15  46

Write a comment


Name Email Message