1. Write a C program to read and print elements of array.
#include<conio.h>
#include<stdio.h>
int main()
printf("1. Write a C program to read and print elements of array.\n");
int a[4];
int i;
printf("enter the elements\n");
for(i=0;i<4;i++)
scanf("%d",&a[i]);
printf("The elements of array are\n");
for(i=0;i<4;i++)
printf("%d\n",a[i]);
return 0;
2. Write a C program to print all negative elements in an array.
#include<conio.h>
#include<stdio.h>
int main()
printf("2. Program to find the -ve elements of all array elements.\n");
int a[5];
int i;
printf("enter the elements\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("The -ve elements of array are\n");
for(i=0;i<5;i++)
if (a[i]<0)
printf("%d\n",a[i]);
return 0;
3. Write a C program to find the sum of all array elements.
#include<conio.h>
#include<stdio.h>
int main()
printf("3. Program to find the sum of all array elements.\n");
int a[5];
int i, sum=0;
printf("enter the elements\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
{
sum = sum +a[i];
printf("the sum of all elements =%d",sum);
return 0;
4. Write a C program to find the second largest element in an array.
#include<conio.h>
#include<stdio.h>
int main()
printf("4. Program to find the second largest element in an array. \n");
int a[5];
int i, g=0,sum=0,m,p;
printf("enter the elements\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
if(a[i+1]>a[i])
g=a[i+1];
sum = sum +a[i];
m=sum/5.0;
for(i=0;i<5;i++)
if(a[i]>m&&a[i]<g)
p= a[i];
sum = sum +a[i];
printf("the 2nd largest elements =%d",p);
return 0;
5. Write a program to display the average of elements in array.
#include<conio.h>
#include<stdio.h>
int main()
printf("5. Program to display the average of elements in array.\n");
int a[5];
int i, sum=0,m;
printf("enter the elements\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
sum = sum +a[i];
m=sum/5.0;
printf("the average of elements =%d",m);
return 0;
6. Write a program to reverse the array.
#include<conio.h>
#include<stdio.h>
int main()
printf("6. Program to reverse the array.\n");
int a[5];
int i;
printf("enter the elements\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("The reverse array are\n");
for(i=4;i>=0;i--)
printf("%d\n",a[i]);
return 0;
7. Write a program to sort the elements of array using selection sort.
#include<stdio.h>
int main()
printf("7. Write a program to sort the elements of array using Bubble sort.\n");
int n,i ;
printf("Enter Number Of Array Elements:");
scanf("%d",&n);
int ar[n],temp,j;
printf("Enter Array Elements:");
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
printf("Before Sorting:");
for(i=0;i<n;i++)
printf("%d\t", ar[i]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(ar[j]>ar[j+1])
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
printf("\nAfter Sorting:\n");
for(i=0;i<n;i++)
printf("%d\t",ar[i]);
}
return 0;
8. Write a program to sort the elements of array using bubble sort.
#include<stdio.h>
int main()
printf("8. Write a program to sort the elements of array using Selection sort. \n");
int n,i ;
printf("Enter Number Of Array Elements:");
scanf("%d",&n);
int ar[n],temp,j,po;
printf("Enter Array Elements:");
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
printf("Before Sorting:");
for(i=0;i<n;i++)
printf("%d\t", ar[i]);
for(i=0;i<n;i++)
po=i;
for(j=i+1;j<n;j++)
if(ar[po]>ar[j])
{
po=j;
temp=ar[i];
ar[i]= ar[po];
ar[po]=temp;
printf("\nAfter Sorting:\n");
for(i=0;i<n;i++)
printf("%d\t",ar[i]);
return 0;
9. Write a program to print maximum and minimum element from the given array.
#include<stdio.h>
int main()
printf("9. Write a program to print maximum and minimum element from the given array.\n");
int n,i ;
printf("Enter Number Of Array Elements:");
scanf("%d",&n);
int ar[n],temp,j;
printf("Enter Array Elements:");
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(ar[j]>ar[j+1])
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
printf("\nMaximum Element = %d",ar[n-1]);
printf("\nMinimum Element = %d",ar[0]);
return 0;
10. Write a program to swap the element of two different positions from the given array.
#include<stdio.h>
int main()
printf("10. Write a program to swap the element of two different positions from the given
array.\n");
int n,i;
printf("Enter Number of Array Elements:");
scanf("%d",&n);
int ar[n],temp;
printf("Enter Array Elements:");
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
printf("Array Elements:");
for(i=0;i<n;i++)
printf("%d\t",ar[i]);
temp=ar[n-1];
ar[n-1]= ar[0];
ar[0]=temp;
printf("\nArray Elements After swaping positions:\n");
for(i=0;i<n;i++)
printf("%d\t",ar[i]);
return 0;
11. Write a C program to count the total number of even and odd elements in an array.
#include<stdio.h>
int main()
printf("11. Write a C program to count the total number of even and odd elements in an array.\
n");
int n,i;
printf("Enter Number of Array Elements:");
scanf("%d",&n);
int ar[n],co=0,ce=0;
printf("Enter Array Elements:");
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
printf("Array Elements:");
for(i=0;i<n;i++)
printf("%d\t",ar[i]);
for(i=0;i<n;i++)
if(ar[i] % 2 ==0)
ce=ce+1;
else
co=co+1;
printf("\nNumber of Even Elements =%d",ce);
printf("\nNumber of Odd Elements =%d",co);
return 0;
}
12. Write a C program to count the total number of negative elements in an array.
#include<stdio.h>
int main()
printf("12. Write a C program to count the total number of negative elements in an array.\n");
int n,i;
printf("Enter Number of Array Elements:");
scanf("%d",&n);
int ar[n],Neg=0;
printf("Enter Array Elements:");
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
for(i=0;i<n;i++)
if(ar[i] <0)
Neg=Neg+1;
printf("\nNumber of Negative Elements=%d",Neg);
return 0;
13. Write a C program to copy all elements from an array to another array.
#include<stdio.h>
int main()
printf("13. Write a C program to copy all elements from an array to another array.\n");
int n,i;
printf("Enter Number of Array Elements:");
scanf("%d",&n);
int ar[n],br[n];
printf("Enter Array Elements:");
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
printf(" First Array Elements:\n");
for(i=0;i<n;i++)
printf("%d\t",ar[i]);
for(i=0;i<n;i++)
br[i]=ar[i];
printf("\nSecond Array Elements:\n");
for(i=0;i<n;i++)
printf("%d\t",br[i]);
return 0;
}
14. Write a C program to print all unique elements in the array. #include<stdio.h>
int main()
printf("14. Write a C program to print all unique elements in the array.\n");
int n,i ;
printf("Enter Number Of Array Elements:");
scanf("%d",&n);
int ar[n],temp,j;
printf("Enter Array Elements:");
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
printf("Array Elements\n");
for(i=0;i<n;i++)
printf("%d\t", ar[i]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(ar[j]>ar[j+1])
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
printf("\nPrinting Unique Array Elements:\n");
for(i=0;i<n;i++)
if(ar[i] != ar[i+1])
printf("%d\t",ar[i]);
return 0;
15. Write a C program to put even and odd elements of array in two separate arrays.
#include<stdio.h>
int main()
printf("15. Write a C program to put even and odd elements of array in two separate
arrays.\n");
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int evenArr[n], oddArr[n];
int evenCount = 0, oddCount = 0,i;
for (i = 0; i < n; i++)
if (arr[i] % 2 == 0)
{
evenArr[evenCount] = arr[i];
evenCount++;
} else {
oddArr[oddCount] = arr[i];
oddCount++;
printf("Even elements: ");
for (i = 0; i < evenCount; i++)
printf("%d ", evenArr[i]);
printf("\n");
printf("Odd elements: ");
for (i = 0; i < oddCount; i++)
printf("%d ", oddArr[i]);
printf("\n");
return 0;
16. Write a C program to add two matrices.
#include<stdio.h>
int main()
printf("16. Write a C program to add two matrices.\n");
int r,c;
printf("Enter Number of Row and Column:");
scanf("%d%d",&r,&c);
int mat1[r][c],mat2[r][c], i,j,sum[r][c];
printf("Enter Matrix-1:");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mat1[i][j]);
printf("Enter Matrix-2:");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mat2[i][j]);
printf("First Matrix:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",mat1[i][j]);
printf("\n");
}
printf("Second Matrix:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",mat2[i][j]);
printf("\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
sum[i][j]=mat1[i][j] + mat2[i][j];
printf("Sum of Matrix:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",sum[i][j]);
printf("\n");
17. Write a C program to subtract two matrices.
#include<stdio.h>
int main()
printf("17. Write a C program to subtract two matrices.\n");
int r,c;
printf("Enter Number of Row and Column:");
scanf("%d%d",&r,&c);
int mat1[r][c],mat2[r][c], i,j,dif[r][c];
printf("Enter Matrix-1:");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mat1[i][j]);
printf("Enter Matrix-2:");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mat2[i][j]);
printf("First Matrix:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
printf("%d\t",mat1[i][j]);
printf("\n");
printf("Second Matrix:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",mat2[i][j]);
printf("\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
dif[i][j]=mat1[i][j] - mat2[i][j];
printf("Difference of Matrix:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",dif[i][j]);
}
printf("\n");
18. Write a C program to perform Scalar matrix multiplication.
#include<stdio.h>
int main()
printf("18. Write a C program to perform Scalar matrix multiplication.\n");
int matrix[10][10], result[10][10];
int rows, columns, scalar;
int i, j;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &columns);
printf("Enter elements of the matrix:\n");
for (i = 0; i < rows; ++i) {
for (j = 0; j < columns; ++j) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
printf("Enter scalar value: ");
scanf("%d", &scalar);
for (i = 0; i < rows; ++i) {
for (j = 0; j < columns; ++j) {
result[i][j] = matrix[i][j] * scalar;
printf("Resultant Matrix after scalar multiplication:\n");
for (i = 0; i < rows; ++i) {
for (j = 0; j < columns; ++j) {
printf("%d\t", result[i][j]);
printf("\n");
return 0;
19. Write a C program to find the sum of each row and column of a matrix.
#include<stdio.h>
int main()
printf("19. Write a C program to find the sum of each row and column of a matrix.\n");
int a[2][2],i,j,sum,matrix;
printf("Enter array elements: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
}
printf("Sum of each row:\n");
for (i = 0; i < 2; ++i)
int rowSum = 0;
for (j = 0; j < 2; ++j)
rowSum += a[i][j];
printf("Row %d: %d\n", i + 1, rowSum);
printf("Sum of each column:\n");
for (j = 0; j < 2; ++j)
int colSum = 0;
for (i = 0; i < 2; ++i)
colSum += a[i][j];
printf("Column %d: %d\n", j + 1, colSum);
return 0;
20. Write a C program to find transpose of a matrix.
#include<stdio.h>
int main()
printf("20. Write a C program to find transpose of a matrix.\n");
int r,c,i,j;
printf("Enter Number of Rows And Columns: ");
scanf("%d%d",&r,&c);
int mat[r][c];
printf("Enter Matrix Elements:");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mat[i][j]);
printf("\nMatrix:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",mat[i][j]);
printf("\n");
printf("\nTranspose Of Matrix::\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",mat[j][i]);
printf("\n");
}
21. Write a C program to find determinant of a matrix.
#include<stdio.h>
int main()
printf("21. Write a C program to find determinant of a matrix.\n");
int m[2][2], determinant = 0,i,j;
printf("Enter the 2 * 2 Matrix Elements: ");
for(i= 0; i < 2; i++)
for(j= 0; j< 2; j++)
scanf("%d", &m[i][j]);
determinant = (m[0][0] * m[1][1]) - (m[0][1] * m[1][0]);
printf("\n The Determinant of 2 * 2 Matrix = %d", determinant);
return 0;
22. Write a program to sort a set of names stored in an array in alphabetical order.
#include<stdio.h>
int main()
printf("22. Write a program to sort a set of names stored in an array in alphabetical order.\n");
int i,j,n;
char str[100][100],s[100];
printf("Enter number of names :");
scanf("%d",&n);
printf("Enter names in any order:");
for(i=0;i<n;i++)
scanf("%s",str[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(strcmp(str[i],str[j])>0)
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
printf("The sorted order of names are:\n");
for(i=0;i<n;i++)
printf("%s\n",str[i]);
23. Write a program to illustrate the use of the following function
a. Strrev()
#include<stdio.h>
#include<string.h>
int main()
printf("a. Strrev()\n");
char Name[10];
printf("Enter Your Name:");
gets(Name);
printf("Reverse of Name:%s",strrev(Name));
return 0;
b. Strcmp()
#include<stdio.h>
#include<string.h>
int main()
printf("b. Strcmp()\n");
char Name[10],Name2[10];
int ret;
printf("Enter Name:");
gets(Name);
printf("Enter Your Name2:");
gets(Name2);
ret=strcmp(Name,Name2);
if(ret == 0)
printf("Both String Are Equal.");
}
else
printf("Strings Are Not Equal.");
return 0;
c. Strcat()
#include<stdio.h>
#include<string.h>
int main()
printf("c. Strcat()\n");
char Name[10],Name2[10];
printf("Enter Name:");
gets(Name);
printf("Enter Your Name2:");
gets(Name2);
printf("Use of Strcat():%s",strcat(Name,Name2));
d. Strcpy()
#include<stdio.h>
#include<string.h>
int main()
printf("d. Strcpy()\n");
char Name[10],Name2[10];
printf("Enter Name:");
gets(Name);
strcpy(Name2,Name);
printf("Use of Strcpy():%s",Name2);
e. Strupr()
#include<stdio.h>
#include<string.h>
int main()
printf("e. Strupr()\n");
char Name[10];
printf("Enter Name:");
gets(Name);
printf("Use of Strupr():%s",strupr(Name));
f. Strlwr()
#include<stdio.h>
#include<string.h>
int main()
printf("f. Strlwr()\n");
char Name[10];
printf("Enter Name:");
gets(Name);
printf("Use of Strlwr():%s",strlwr(Name));