Example : A c program to read & display the nine values in a two dimensional array.

#include<stdio.h>
#include<conio.h>
void main()
{
	int x[3][3],i,j;
	printf("Enter 9 values:-");
	for(i=0;i<=2;i++)
	{
		for(j=0;j<=2;j++)
		{		
			scanf("%d",&x[i][j]);
		}
	}
	
	printf("\nThe nine values in two dimensional forms are :\n");
	for(i=0;i<=2;i++)
	{
		for(j=0;j<=2;j++)
		{		
			printf("%d\t",x[i][j]);			
		}
		printf("\n");
	}
	getch();
}

Example : Write a program in c to display the addition result of two matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
    int x[3][3],y[3][3],z[3][3];
    int i,j;
    printf("Enter 9 values in the first matrix of size 3x3: \n");
    
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            scanf("%d", &x[i][j]);
        }
    }
   
    printf("Enter 9 values in the second matrix of size 3x3: \n");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            scanf("%d", &y[i][j]);
        }
    }
    
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            z[i][j] = x[i][j] + y[i][j];
        }
    }
    
    printf("\n Adition of Two matrices are = \n");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            printf("%d\t", z[i][j]);
        }
        printf("\n");
    }
    getch();
}

Example : Write a program in c to display the subtraction result of two matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
    int x[3][3],y[3][3],z[3][3];
    int i,j;
    printf("Enter 9 values in the first matrix of size 3x3: \n");
    
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            scanf("%d", &x[i][j]);
        }
    }   
    printf("Enter 9 values in the second matrix of size 3x3: \n");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            scanf("%d", &y[i][j]);
        }
    }    
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            z[i][j] = x[i][j] - y[i][j];
        }
    }    
    printf("\n Adition of Two matrices are = \n");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            printf("%d\t", z[i][j]);
        }
        printf("\n");
    }
    getch();
}

Example : Write a program in c to display the addition result of only odd numbers in a two dimensional array .

#include<stdio.h>
#include<conio.h>
void main()
{
	int x[3][3],i,j,sum=0;
	printf("Enter nine no.:-");
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
		   scanf("%d",&x[i][j]);
		}
	}
	for(i=0; i<3; i++)
        {
           for(j=0; j<3; j++)
             {
                printf("%d\t",x[i][j]);            
             }
             printf("\n");        
        }
	 
	printf("\nThe odd values are = ");
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			if(x[i][j]%2!=0)
			{
			    printf("%d\t",x[i][j]);	
			    sum=sum+x[i][j];
			}			
		}	
	}	
	printf("\nSum of odd numbers are = %d\t",sum);
	getch();	
}

Example : Write a program in c to display the transpose form of matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
    int x[3][3],y[3][3];
    int i,j;
    printf("Enter 9 values in the first matrix of size 3x3: \n");
    
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            scanf("%d", &x[i][j]);
        }
    }
    
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            y[j][i] = x[i][j];
        }
    }
    
    printf("\n The Matrix values are = \n");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            printf("%d\t", x[i][j]);
        }
        printf("\n");
    }
    
    printf("\n Transpose of Matrix are = \n");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            printf("%d\t", y[i][j]);
        }
        printf("\n");
    }
    getch();
}

NB : Transpose matrix is the matrix in which each row values of a normal matrix are seen in each column.

Example : Write a program in c to display the addition result of each row and each column values in a matrix.

#include<stdio.h>
#include<conio.h>

void main()
{
    int x[3][3];
    int i,j,sum;
    printf("Enter 9 values in the first matrix of size 3x3: \n");
    
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            scanf("%d", &x[i][j]);
        }
    }
	
    printf("\nThe Matrix values are =\n");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            printf("%d\t", x[i][j]);
        }
        printf("\n");
    }	    
    
    printf("\nThe Addition of row values are =\n\n");    
    for(i=0; i<3; i++)
    {
        sum=0;
	for(j=0; j<3; j++)
        {
            sum=sum+x[i][j];			
        }
        printf("Sum of Row Elements are %d = %d\n", i+1, sum);
    }
    
    printf("\nThe Addition of column values are =\n\n");
    for(i=0; i<3; i++)
    {
        sum=0;
	for(j=0; j<3; j++)
        {
            sum=sum+x[j][i];		
        }
        printf("Sum of Column elements are %d = %d\n", j+1, sum);
    }

    getch();
}

Example : Write a program in c to display the diagonal values along with its sum of a matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
    int x[3][3];
    int i,j,sum1=0,sum2=0;
    printf("Enter 9 values in the first matrix of size 3x3: \n");
    
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            scanf("%d", &x[i][j]);
        }
    }
    
    printf("\nThe Matrix values are \n");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            printf("%d\t",x[i][j]);            
        }
        printf("\n");        
    } 
	
    printf("\nThe first/main diagonal values are = ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if(i==j)
            printf("%d",x[i][j]);
            sum1=sum1+x[i][j];            
        }        
    } 
	
    printf("\nThe second diagonal values are = ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if(i+j==2)
            printf("%d",x[i][j]);
            sum2=sum2+x[i][j];            
        }        
    }       
   printf("\n\nThe first/main diagonal values sum are = %d\n",sum1);
   printf("The second diagonal values sum are = %d",sum2);

   getch();
}

 549 total views,  1 views today

Categories: C

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.