Thursday, May 29, 2025
c program to know a matrix is identity or not | matrix examples in c
c program to print sum of two matrices of order mxn | 2D Array-matrix examples
Array programs in C
/* to find sum of two matrix of order mxn
*/
#include<stdio.h>
int main()
{
int m,n,i,j;
printf("enter m as rows and n as columns\n");
scanf("%d%d",&m,&n);//if m=3 then the loop would run for 0,1,2
int m1[m][n],m2[m][n];
printf("enter elements for first matrix m1\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("enter element for location %d %d\n",i,j);
scanf("%d",&m1[i][j]);
}
}
printf("enter elements for second matrix m2\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("enter element for location %d %d\n",i,j);
scanf("%d",&m2[i][j]);
}
}
printf("the sum of two matrices is:\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d",m1[i][j]+m2[i][j]);
}
printf("\n");
}
return 0;
}
Output:-
c program to print transpose of matrix of order 3x2 | 2D Array-matrix examples
Array programs collections
/* program to find transpose of given matrix of order 3x2
*/
#include<stdio.h>
int main()
{
int matrix[3][2];
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=1;j++)
{
printf("data for %d %d location is:",i,j);
scanf("%d",&matrix[i][j]);
}
}
printf("the matrix before transpose is:\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=1;j++)
{
printf("%d",matrix[i][j]);
}
printf("\n");
}
printf("the matrix after transpose is:\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
{
printf("%d",matrix[j][i]);
}
printf("\n");
}
return 0;
}
Output:
Wednesday, May 28, 2025
c program to print diagonal elements | 2D Array-matrix examples
Array program collection in C
/* program to input elements of a
matrix of order 3x3 and print only main diagonal elements
Output:-
c program to initialize 2D array | 2D Array examples
2D array programs
/* program to initialize 2D array*/
#include<stdio.h>int main()
{
int a[2][2]={{12,13},{14,15}};
int i,j;
for(i=0;i<=1;i++)//for row
{
for(j=0;j<=1;j++)//for columns
{
printf("%3d",a[i][j]);
//%3d means upto 3 trailing spaces
}
printf("\n");
}
return 0;
}
output:-
Tuesday, May 27, 2025
c program to copy array elements to another | Array examples
Array programs in C
/* program to copy one array elements to another
Output:-
Monday, May 19, 2025
C Program to Input elements and to find average value using array-1D array concept
C program to input elements and to find average
//array input and printing average value
#include<stdio.h>
int main()
{
int a[5];//array declaration
int i,sum=0;//variable declaration
float average;//float to store average
for(i=0;i<=4;i++)
{
printf(" enter value for location=%d\n",i+1);
scanf("%d",&a[i]);//input for given array
sum+=a[i]; //finding sum
}
average=(float)sum/5; //type casting and storing average in variable
printf("the average value is=%f",average);//printng result
return 0;
}
Output:-
second method:
//array input and printing average value. Here, we will use define for size.
#include<stdio.h>
#define size 5
int main()
{
int a[size];//array declaration with size as an index value. It takes the value from define
int i,sum=0;//variable declaration
float average;//float to store average
for(i=0;i<=4;i++)
{
printf(" enter value for location=%d\n",i+1);
scanf("%d",&a[i]);//input for given array
sum+=a[i]; //finding sum
}
average=(float)sum/5; //type casting and storing average in variable
printf("the average value is=%f",average);//printng result
return 0;
}
Sunday, May 18, 2025
C Program – 1D Array Input and Output with Simple Examples
array input in C program
// 1Dimensional (1D) array input
#include<stdio.h>
int main()
{
int a[5];//array declaration of size 5
int i;
for(i=0;i<=4;i++)//loop
{
printf(" enter value for location=%d\n",i+1);//message display
scanf("%d",&a[i]);//input
}
for(i=0;i<=4;i++)
{
printf("%d\t",a[i]);//printing the values
}
return 0;
}
Output:-