Table of Contents hide
Example : A c program to read & display the nine values in a two dimensional array having size 3×3.
#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 : A C program to read nine values & display only first and last values in a two dimensional array having size 3×3.
#include<stdio.h>
#include<conio.h>
void main()
{
	int x[3][3],i,j;
	printf("Enter nine no.:-\n");
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
		   scanf("%d",&x[i][j]);
		}
	}
	printf("\nThe First and Last array values are :-\n");
	
	printf("\nThe First value is = %d",x[0][0]);
	printf("\nThe Last value is = %d",x[2][2]);
	getch();	
}
Example : Write a program in C to display the addition of two matrices into third one.
#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 of two matrices values into third one.
#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 (sum total) of only odd numbers of 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 largest and smallest value from a two dimensional array of size 3×3.
#include<stdio.h>
#include<conio.h>
void main()
{
	int x[3][3],i,j,largest,smallest;
	printf("Enter nine no.:-\n");
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
		   scanf("%d",&x[i][j]);
		}
	}
	printf("\nThe two dimensional array values are :-\n");
	for(i=0; i<3; i++)
        {
           for(j=0; j<3; j++)
             {
                printf("%d\t",x[i][j]);            
             }
             printf("\n");        
        }	 
	
	largest=x[0][0];
	smallest=x[0][0];
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			if(largest < x[i][j])
			{
			    largest = x[i][j];
			}
			
			if(smallest > x[i][j])
			{
			    smallest = x[i][j];
			}			
		}	
	}	
	printf("\nThe Largest value is = %d",largest);
	printf("\nThe Smallest value is = %d",smallest);
	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 only second row values from a two dimensional array of size 3x 3.
#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("\nOnly the Second Row values in two dimensional forms are :\n");
	for(i=0;i<=2;i++)
	{
		for(j=0;j<=2;j++)
		{	
			if(i==1)
			{
				printf("%d\t",x[i][j]);	
			}		
		}
		printf("\n");
	}
	
	getch();
}
Example : Write a program in C to display only third column values from a two dimensional array of size 3x 3.
#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("\nOnly the Second Row values in two dimensional forms are :\n");
	for(i=0;i<=2;i++)
	{
		for(j=0;j<=2;j++)
		{	
			if(j==2)
			{
				printf("%d\t",x[i][j]);	
			}		
		}
		printf("\n");
	}
	
	getch();
}
Example : Write a program in C to display the addition 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 only the diagonal (both – left and right) 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();
}
Example : Write a program in C to display all the values except diagonal (both left and right) along with its sum in a matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
    int x[3][3];
    int i,j,sum1=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 values except first/main and second diagonal are = ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if(i!=j && i+j!=2)
            {
            	printf(" %d ",x[i][j]);
            	sum1=sum1+x[i][j];	
	    }                        
        }        
    }       
   printf("\n\nThe sum of all the values except first/main and second diagonal of a matrix is = %d\n",sum1); 

   getch();
}
Example : Write a program in C to display only common value present in the diagonal (both – left and right) in a matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
    int x[3][3];
    int i,j;
    printf("Enter 9 values in the 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 value common in both the diagonal is  = ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if(i==j && i+j==2)
            {
            	printf(" %d ",x[i][j]);            		
	    }                        
        }        
    }  
  getch();
}
Example : Write a program in C to display only border values of a matrix having size 4×4.
#include<stdio.h>
#include<conio.h>
void main()
{
    int x[4][4];
    int i,j;
    printf("Enter 16 values in the matrix of size 4x4: \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 border values of a matrix are  = ");
    for(i=0; i<=3; i++)
    {
        for(j=0; j<=3; j++)
        {
            if(i==0 || i==3 || j==0 || j==3)
            {
            	printf(" %d ",x[i][j]);            		
	    }                        
        }        
    }
   getch();
}
Example : Write a program in C to display only center/core (except border) values of a matrix having size 4×4.
#include<stdio.h>
#include<conio.h>
void main()
{
    int x[4][4];
    int i,j;
    printf("Enter 16 values in the matrix of size 4x4: \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 border values of a matrix are  = ");
    for(i=0; i<=3; i++)
    {
        for(j=0; j<=3; j++)
        {
            if(i!=0 && i!=3&& j!=0 && j!=3)
            {
            	printf(" %d ",x[i][j]);            		
	    }                        
        }        
    }
   getch();
}

Loading

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.