Skip to main content

Matrix Subtraction

// Matrix Subtraction

#include<stdio.h>
#define SIZE 5
int main()
{
    int a[SIZE][SIZE],b[SIZE][SIZE];
    int i,j,r1,r2,c1,c2;

    printf("Enter the Rows of Matrix - A : ");
    scanf("%d",&r1);
    printf("Enter the Columns of Matrix - A : ");
    scanf("%d",&c1);

    printf("Enter the Rows of Matrix - B : ");
    scanf("%d",&r2);
    printf("Enter the Columns of Matrix - B : ");
    scanf("%d",&c2);

    if(c1 == r2)
    {
        printf("Enter Elements of Matrix - A\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
            {
                printf("a[%d][%d] : ",i,j);
                scanf("%d",&a[i][j]);
            }
        }

        printf("Enter Elements of Matrix - B\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
            {
                printf("b[%d][%d] : ",i,j);
                scanf("%d",&b[i][j]);
            }
        }

        printf("\n Matrix - A\n");
        for(i=0;i<r1;i++)
        {
        printf("|");
            for(j=0;j<c1;j++)
            {
                printf("%3d",a[i][j]);
            }
            printf(" | \n");
        }

        printf("\n Matrix - B\n");
        for(i=0;i<r2;i++)
        {
        printf("|");
            for(j=0;j<c2;j++)
            {
                printf("%3d",b[i][j]);
            }
            printf(" | \n");
        }

        printf("\nMatrix Subtraction [A-B]\n");
        for(i=0;i<r1;i++)
        {
        printf("|");
            for(j=0;j<c2;j++)
            {
                printf("%3d",a[i][j] - b[i][j]);
            }
            printf(" | \n");
        }
    }
    else
    {
        printf("Enter Valid Rows and Columns");
    }
    return 0;
}



Comments

Popular posts from this blog

Search an Element

// Searching an Element #include<stdio.h> #define SIZE 10 void search(int a[],int no) {    int i, flag = 0;    for(i=0;i<=a[i];i++)    {        if(no == a[i])        {            printf("%d Found at Position %d And Index No is %d",no,i+1,i);            flag=1;        }    }    if(!flag)    {        printf("%d not Found!!",no);    } } int main() {    int i,no,elements;    int a[SIZE];    printf("Enter a Number of Elements : ");    scanf("%d",&elements);    for(i=0;i<elements;...

Lower Triangular Matrix

// Lower Triangular Matrix #include<stdio.h> int main() {     int i,j,k,m,s=1,rc;     printf("Enter the No of Raws and Columns : ");     scanf("%d",&rc);       for(i=1;i<rc;i++)     {         for(m=0;m<i;m++) // for loop which prints numbers.         {             printf("%3d",s);             s++;         }         for(j=rc;j>i;j--) // for loop which prints 0.         {             printf("  0");         }         printf("\n");     }     return 0; }