Sponsors

Adding Two Matrices Vertically and Horizontally in C Language

https://gyazo.com/54244c07bf4feb317edd9662835e4a0e

 

Hi guys , I just tried to code to add two matrices vertically and horizontally .
Check the screenshot I’ve attached above .

I hope you like it , and just let me know in comment if you have any better solution .

 

#include<stdio.h>
int main()
{
int m,n,i,j;
printf(“Rows of Matrix: “);
scanf(“%d”, &m);
printf(“Column of Matrix: “);
scanf(“%d”, &n);
int ar[m][n], arr[m][n];

printf(“First Matrix: \n”);

for(i=0; i<m; i++){
for(j=0; j<n; j++){
scanf(“%d”, &ar[i][j]);
}
printf(“\n”);
}

printf(“Second Matrix : \n”);

for(i=0; i<m; i++){
for(j=0; j<n; j++){
scanf(“%d”, &arr[i][j]);
}
printf(“\n”);
}

int ar1[m+m][n], ar2[m][n+n],p=0;

for(i=0; i<2*m; i++){
if(i<m){
for(j=0; j<n; j++){
ar1[i][j]=ar[i][j];
}
}
else{
for(j=0; j<n; j++){
ar1[i][j]=arr[p][j];
}
p++;
}
}

for(i=0; i<m; i++){
p=0;
for(j=0; j<2*n; j++){
if(j<n)
ar2[i][j]=ar[i][j];

else{

ar2[i][j]=arr[i][p];
p++;
}

}
}

printf(“Vertically Added Matrix”);
printf(“\n”);
for(i=0; i<2*m; i++){
for(j=0; j<n; j++)
printf(“%d  “, ar1[i][j]);
printf(“\n”);
}

printf(“\n”);
printf(“Horizontally Added Matrix”);
printf(“\n”);
for(i=0; i<m; i++){
for(j=0; j<2*n; j++)
printf(“%d  “, ar2[i][j]);
printf(“\n”);
}

return 0;
}

The Ultimate Guide to...

I discovered the Keyboard and Car Universal Cleaning Gel...

Why Everyone is Obsessed with the Electric Spin Scrubber Pro

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Mini Magnetic Bag Sealer

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Electric Spin Scrubber Pro

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Sunset Projection Lamp LED

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Electric Spin Scrubber Cordless

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

The Ultimate Guide to the Keyboard and Car Universal Cleaning Gel

I discovered the Keyboard and Car Universal Cleaning Gel and it is a total game-changer for my daily routine. Here is why you need...

The Ultimate Guide to the Mini Portable Bag Sealer Magnetic

I discovered the Mini Portable Bag Sealer Magnetic and it is a total game-changer for my daily routine. Here is why you need it... ...

Why you need the Portable Door Lock Travel Home Security

The Portable Door Lock Travel Home Security is a top trending find. 🛍️ VIEW LIVE PRICES ON AMAZON ...

Quote of the day by Charles Darwin: “If I had my life to live over again, I would have made a rule to read...

The Profound Wisdom of Charles Darwin’s Regret: A Lesson for 2026Charles Darwin is globally celebrated for his revolutionary contributions to science and his theory...