Friday, 10 July 2020

2D-Multiplication with function | C Language

Q : Write a function to display the multiplication table of the number.

Answer : 

#include<stdio.h>
#include<conio.h>
/*============================
     Girfa Student Help
     Multiplication 2d array using function
==============================*/
void multi(int ar1[3][3],int ar2[3][3],int ar3[3][3])
{

     int r,c,m,tmp;

     for (r = 0; r<3; r++)
     {
           for (c = 0; c<3; c++)
           {
                for (m = 0, tmp = 0; m<3; m++)
                     tmp += ar1[r][m] * ar2[m][c];
                ar3[r][c] = tmp;
           }
     }
}
void main()
{

     int ar1[3][3], ar2[3][3], ar3[3][3], r, c, m, tmp;
     clrscr();
     printf("first array input\n");
     for (r = 0; r<3; r++)
     {
           for (c = 0; c<3; c++)
           {
                printf("Enter no ");
                scanf("%d", &ar1[r][c]);
           }
     }
     printf("second array\n");
     for (r = 0; r<3; r++)
     {
           for (c = 0; c<3; c++)
           {
                printf("Enter no ");
                scanf("%d", &ar2[r][c]);
           }
     }
     /*  Calling Multiplication Function */
     multi(ar1,ar2,ar3);
     clrscr();
     printf("\nFirst array\n");
     for (r = 0; r<3; r++)
     {
           for (c = 0; c<3; c++)
           {
                printf("\t%d", ar1[r][c]);

           }
           printf("\n");
     }
     printf("\nSecond array\n");
     for (r = 0; r<3; r++)
     {
           for (c = 0; c<3; c++)
           {
                  printf("\t%d", ar2[r][c]);

           }
           printf("\n");
     }
     printf("\nMultiplication of array\n");
     for (r = 0; r<3; r++)
     {
           for (c = 0; c<3; c++)
           {
                printf("\t%d", ar3[r][c]);
           }
           printf("\n");
     }

     getch();


}

No comments:

Post a Comment