Wednesday, April 3, 2024

Matrix multiplication in C

 


Matrix multiplication in C: We can add, subtract, multiply and divide 2 matrices. To do so, we are taking input from the user for row number, column number, first matrix elements and second matrix elements. Then we are performing multiplication on the matrices entered by the user.

In matrix multiplication first matrix one row element is multiplied by second matrix all column elements.

Let's try to understand the matrix multiplication of 2*2 and 3*3 matrices by the figure given below:

matrix multiplication program in c

Let's see the program of matrix multiplication in C.

  1. #include<stdio.h>    
  2. #include<stdlib.h>  
  3. int main(){  
  4. int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;    
  5. system("cls");  
  6. printf("enter the number of row=");    
  7. scanf("%d",&r);    
  8. printf("enter the number of column=");    
  9. scanf("%d",&c);    
  10. printf("enter the first matrix element=\n");    
  11. for(i=0;i<r;i++)    
  12. {    
  13. for(j=0;j<c;j++)    
  14. {    
  15. scanf("%d",&a[i][j]);    
  16. }    
  17. }    
  18. printf("enter the second matrix element=\n");    
  19. for(i=0;i<r;i++)    
  20. {    
  21. for(j=0;j<c;j++)    
  22. {    
  23. scanf("%d",&b[i][j]);    
  24. }    
  25. }    
  26.     
  27. printf("multiply of the matrix=\n");    
  28. for(i=0;i<r;i++)    
  29. {    
  30. for(j=0;j<c;j++)    
  31. {    
  32. mul[i][j]=0;    
  33. for(k=0;k<c;k++)    
  34. {    
  35. mul[i][j]+=a[i][k]*b[k][j];    
  36. }    
  37. }    
  38. }    
  39. //for printing result    
  40. for(i=0;i<r;i++)    
  41. {    
  42. for(j=0;j<c;j++)    
  43. {    
  44. printf("%d\t",mul[i][j]);    
  45. }    
  46. printf("\n");    
  47. }    
  48. return 0;  
  49. }  

Output:

enter the number of row=3
enter the number of column=3
enter the first matrix element=
1 1 1
2 2 2
3 3 3
enter the second matrix element=
1 1 1
2 2 2
3 3 3
multiply of the matrix=
6 6 6
12 12 12
18 18 18

Let's try to understand the matrix multiplication of 3*3 and 3*3 matrices by the figure given below:

matrix multiplication in c

---------------------------------------------------




2×2 Matrix Multiplication

Let’s consider a simple 2 × 2 matrix multiplication

=[3749]  =[6258]

Now each of the elements of product matrix AB can be calculated as follows:

  • AB11 = 3 × 6 + 7 ×5 = 53
  • AB12 = 3 × 2 + 7 × 8 = 62
  • AB21 = 4 × 6 + 9 × 5 = 69
  • AB22 = 4 × 2 + 9 × 8 = 80

Therefore,

=[53626980]

3×3 Matrix Multiplication

To understand the multiplication of two 3 × 3 matrices, let us consider two 3 × 3 matrices A and B.

=[1284317149810], =[519361597816]

Each element of the Product matrix AB can be calculated as follows:

  • AB11 = 12×5 + 8×6 + 4×7 = 136
  • AB12 = 12×19 + 8×15 + 4×8 = 380
  • AB13 = 12×3 + 8×9+4×16 = 172
  • AB21 = 3×5 + 17×6 + 14×7 = 215
  • AB22 = 3×19 + 17×15 + 14×8 = 424
  • AB23 = 3×3 + 17×9 + 14×16 = 386
  • AB31 = 9×5 + 8×6 + 10×7 = 163
  • AB32 = 9×19 + 8×15 + 10×8 = 371
  • AB33 = 9×3 + 8×9 + 10×16 = 259

Therefore,


//C program to check whether a number is divisible by 2 and 9 or not


#include <stdio.h>

int main()

{

    int num;   

    printf("Enter any number: ");

    scanf("%d", &num);


//if(!(num % 2) && !(num % 9))

    if((num % 2 == 0) && (num % 9 == 0))

    {

        printf("Number is divisible by 2 and 9");

    }

    else

    {

        printf("Number is not divisible by 2 and 9");

    }

    return 0;

}


//Enter any number: 18

//Number is divisible by 2 and 9


C Program To Check Number Is Divisible By 5 and 11 or Not | C Programs

 C program to check whether a number is divisible by 5 and 11 or not – In this article, we will detail in on the only way to determine whether a number is divisible by 5 and 11 or not.

Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.

As you can see, these photos describe how a number is divisible by 5 and 11 respectively.

Divisible by 5: A number is divisible by 5 if its Unit digit is 5 or 0. For e.g. 20, 55, 75 etc.

Divisible by 11: A number is divisible by 11 if the difference between the sum of its even digits and odd digits is 0 or a multiple of 11.

The combination of these would make the number divisible by 5 and 11 both at the same time.

Thus, the only way to find out whether a number is divisible by 5 and 11 in C programming here is as follows:

Using Standard Method

1)Read entered number n using “scanf” function.

2)The remainder of n/5 and remainder of n/11 is 0 then print “it is divisible by 5 and 11”.otherwise print “it is not divisible by 5 and 11”.

Output:

..

Matrix multiplication in C

  Matrix multiplication  in C: We can add, subtract, multiply and divide 2 matrices. To do so, we are taking input from the user for row num...