Little Matrix Multiplication - Easy Problems - Solution to Toph.co Online Judge Problem

Little Matrix Multiplication

Limits 1s, 512 MB

Given two 2×2 matrices, calculate the product of the two matrices.
The product of two 2×2 matrices will produce another 2×2 matrix where:
A = [ A₁₁ A₁₂
      A₂₁ A₂₂ ]
B = [ B₁₁ B₁₂
      B₂₁ B₂₂ ]
C = A × B = [ C₁₁ C₁₂
              C₂₁ C₂₂ ]
C₁₁ = A₁₁ ×  B₁₁ + A₁₂ × B₂₁
C₁₂ = A₁₁ ×  B₁₂ + A₁₂ × B₂₂
C₂₁ = A₂₁ ×  B₁₁ + A₂₂ × B₂₁
C₂₂ = A₂₁ ×  B₁₂ + A₂₂ × B₂₂
For example:
A = [ 2 3
      4 5 ]
B = [ 5 6
      7 8 ]
C = A × B = [ 31 36
              55 64 ]

Input

The input contains two matrices as follows:
A₁₁ A₁₂
A₂₁ A₂₂
B₁₁ B₁₂
B₂₁ B₂₂
The value of each of these 8 integers is non-negative and less than 100.

Output

Print the product of the two matrices as follows:
C₁₁ C₁₂
C₂₁ C₂₂ 
 
#include<stdio.h>

int main()
{
    int a[2][2],b[2][2],sum,i,j,k;
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }

     for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }

     for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            sum=0;
            for(k=0;k<2;k++)
            {
                sum+=a[i][k]*b[k][j];
            }
            printf("%d ",sum);
        }
        printf("\n");
    }
}