-->
Showing posts with label Array Programs. Show all posts
Showing posts with label Array Programs. Show all posts

Thursday, May 29, 2025

c program to know a matrix is identity or not | matrix examples in c

Array programs collection C

/* to know a matrix of order n is identity or not

1 0 0
0 1 0
0 0 1
*/
#include<stdio.h>
int main()
{
int n,i,j,is_Identity=0;
printf("enter the size of matrix\n");
scanf("%d",&n);
int matrix[n][n];
printf("enter elements of given matrix\n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
scanf("%d",&matrix[i][j]);
}
}
for(i=0;i<=n-1;i++)//loop iteration for row
{
for(j=0;j<=n-1;j++)//loop iteration for column
{
if((i==j && matrix[i][j]!=1)||(i!=j && matrix[i][j]!=0))// checking the condition
// two conditons to bes tested
//Check if the current element is on the diagonal (i == j)
        // In the identity matrix, diagonal elements must be 1
        // Also check if the element is not on the diagonal (i != j)
        // In identity matrix, non-diagonal elements must be 0
is_Identity=1;
break;
}
}
printf("the matrix is:\n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%5d",matrix[i][j]);
}
printf("\n");
}
if(is_Identity==0)
{
printf("The matrix is identity");
}
else
{
printf("the matrix is not identity");
}
return 0;
}

Output:-

c program to know a matrix is identity or not



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:

c program to transpose a matrix



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

 1 2 3
 4 5 6
 7 8 9
 the output would be 1
  5
  6
*/
#include<stdio.h>
int main()
{
int a[3][3];//array/matrix declaration of size 3x3
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("enter element for %d %d location",i,j);
scanf("%d",&a[i][j]);//input for matrix
}
}
for(i=0;i<=2;i++)//it is for row
{
for(j=0;j<=2;j++)//it is for column
{
if(i==j) //condition/logic to print the values
printf("%d",a[i][j]);
else
printf(" "); //printing none if the condition is false
}
printf("\n");
}
return 0;
}

Output:-

c program to print diagonal elements



c program to input 2D array element | 2D Array(matrix) examples

 

Matrix program in C[2D examples]




/* 2D array input an output */

#include<stdio.h>
#define row 200
#define column 200
int main()
{
int a[row][column];
int r,c;
int i,j;
printf("enter row and column[0-199] to be used for matrix\n");
scanf("%d%d",&r,&c);//rows and column input
for(i=0;i<=r-1;i++)//for row
{
for(j=0;j<=c-1;j++)//for columns
{
printf("enter element for location %d %d\n",i,j);
scanf("%d",&a[i][j]);
}
}
printf("the matrix is:\n");
for(i=0;i<=r-1;i++)//for row
{
for(j=0;j<=c-1;j++)//for columns
{
printf("%5d",a[i][j]);//printing input with max. width of 5 characters.
}
printf("\n");
}
return 0;
}

Output:-

C program to input elements of mxn matrix



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:-

program to initialize 2D array


Tuesday, May 27, 2025

c program to sort numbers in ascending order | Array examples

C Array programs

C program to sort the numbers



/*1D array to sort elements
if we have 32,12,43,22,5
after sorting, we have 5,12,22,32,43
*/
#include<stdio.h>
int main()
{
int a[5];
int i,j,temp;
for(i=0;i<=4;i++)
{
printf("enter element for location %d",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<=4;i++)
{
for(j=i+1;j<=4;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<=4;i++)
{
printf("%d\n",a[i]);
}
return 0;
}

Output:

c program to sort the numbers



Tuesday, May 20, 2025

c program to print maximum and minimum value | Array examples

 C array programs

//array program to input 5 numbers and to print maximum and minimum value

#include<stdio.h>
#define size 5
int main()
{
int a[size];//array declaration with size 5
int i,max,min;
for(i=0;i<=4;i++)
{
printf(" enter value for location=%d\n",i+1);
scanf("%d",&a[i]);//inputs
}
max=a[0];//assigning first value to max and min
min=a[0];
for(i=0;i<=4;i++)
{
if(max<a[i])//comparing and changing the old value
{
max=a[i];
}
if(min>a[i])//comparing and assigning new value
{
min=a[i];
}
}
printf("the max value=%d and minimum value is %d",max,min);//printing
return 0;
}

Output:-



C program to copy one array element to another | Array examples

Array programs in C

C program to copy one array elements to another 

/* program to copy one array element to another
*/

#include<stdio.h>
int main()
{
int a[5];
int b[5];
int i;
for(i=0;i<=4;i++)
{
printf("enter elment for %d location",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<=4;i++)
{
b[i]=a[i];
}
printf("the copied elements are:\n");
for(i=0;i<=4;i++)
{
printf(" %d ",b[i]);
}
return 0;
}


Output:-

c program to copy elements to another array



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:-

C program to find average of five elements using array



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 – Array Declaration and Initialization | With Examples

 Array initialization in C


// 1D array initialization


#include<stdio.h>
int main()
{
int a[5]={12,23,45,78,55};
//array declaration and initialization
int i;//variable declaration
for(i=0;i<=4;i++)//loop
{
printf("%d\t",a[i]);//printing values
}
return 0;
}


Output:-

1D Array initialization in C