#include<stdio.
h>
int main(){
int arr[10], i, num, n, pos=-1;
printf("Enter the array size: ");
scanf("%d", &n);
printf("Enter Array Elements: ");
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the number to be searched: ");
scanf("%d", &num);
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
pos = i+1;
printf("Element %d found at position %d", num, pos);
break;
}
}
if (pos == -1)
{
printf("Number not found..!!\n");
}
return 0;
}
Output:
Enter the array size: 5 Enter the array size: 5
Enter Array Elements: 7 1 2 6 9 Enter Array Elements: 7 1 2 6 9
Enter the number to be searched: 2 Enter the number to be searched: 8
Element 2 found at position 3 Number not found..!!
ST THOMAS COLLEGE OF ENGINEERING & TECHNOLOGY KOZHUVALLOOR 5
14 10 27 33 35
After last iteration,
10 14 27 33 35
Array is sorted
#include<stdio.h>
int main(){
int n, i, arr[50], j, temp;
printf("Enter total number of elements: ");
scanf("%d", &n);
printf("Enter the elements: ");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
for(i=0; i<(n-1); i++)
{ Output:
for(j=0; j<(n-i-1); j++) Enter total number of elements: 5
{ Enter the elements: 5 1 8 9 3
if(arr[j]>arr[j+1]) Elements sorted successfully..!!
{ Sorted list in ascending order :
temp=arr[j]; 1 3 5 8 9
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("Elements sorted successfully..!!\n");
printf("Sorted list in ascending order :\n");
for(i=0; i<n; i++)
printf("%d\t", arr[i]);
return 0;
}
ST THOMAS COLLEGE OF ENGINEERING & TECHNOLOGY KOZHUVALLOOR 7
Qn) C program to perform the addition of two matrices.
#include<stdio.h> printf("Elements of second matrix:\n");
int main() for ( i = 0; i < m ;i++)
{ {
int m, n, i, j, a[10][10], b [10][10], s[10][10]; for ( j = 0; j < n ;j++)
printf("Enter the number of rows & columns : "); {
scanf("%d%d", &m, &n); printf("%d\t", b[i][j]);
printf("Enter the elements of first matrix\n"); }
for ( i = 0; i < m ;i++) printf("\n");
{ }
for ( j = 0; j < n ;j++) for ( i = 0; i < m ;i++)
{ {
scanf("%d", &a[i][j]); for ( j = 0; j < n ;j++)
} {
} s[i][j] = a[i][j] + b[i][j];
printf("Enter the elements of second matrix\n"); }
for ( i = 0; i < m ;i++) }
{ printf("Resultant matrix:\n");
for ( j = 0; j < n ;j++) for ( i = 0; i < m ;i++)
{ {
scanf("%d", &b[i][j]); for ( j = 0; j < n ;j++)
} {
} printf("%d\t", s[i][j]);
printf("Elements of first matrix:\n"); }
for ( i = 0; i < m ;i++) printf("\n");
{ }
for ( j = 0; j < n ;j++) return 0;
{ }
printf("%d\t", a[i][j]);
}
printf("\n");
}
ST THOMAS COLLEGE OF ENGINEERING & TECHNOLOGY KOZHUVALLOOR 10
Enter the number of rows & columns : 3 3
Enter the elements of first matrix
123456789
Enter the elements of second matrix
10 11 12 13 14 15 16 17 18
Elements of first matrix: Elements of second matrix:
1 2 3 10 11 12
4 5 6 13 14 15
7 8 9 16 17 18
Resultant matrix:
11 13 15
17 19 21
23 25 27
Qn) C program to perform the multiplication of two matrices.
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and columns for first matrix: ");
scanf("%d%d", &r1,&c1);
printf("Enter rows and columns for second matrix: ");
scanf("%d%d", &r2, &c2);
while (c1!=r2)
{
printf("\nError! column of first matrix not equal to row of second.");
printf("Enter rows & columns for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows & columns for second matrix: ");
scanf("%d%d", &r2, &c2);
}
ST THOMAS COLLEGE OF ENGINEERING & TECHNOLOGY KOZHUVALLOOR 11
printf("\nEnter elements of matrix 1: \n");
for(i=0; i<r1; i++) printf("%d\t", b[i][j]);
{ }
for(j=0; j<c1; j++) printf("\n");
{ }
printf("Enter element a%d%d: ",i+1,j+1); //Initializing elements of matrix mult to 0.
scanf("%d", &a[i][j]); for(i=0; i<r1; i++)
} {
} for(j=0; j<c2; j++)
printf("\nEnter elements of matrix 2: \n"); {
for(i=0; i<r2; i++) mult[i][j]=0;
{ for(k=0; k<c1; k++)
for(j=0; j<c2; j++) {
{ mult[i][j] = mult[i][j] + a[i][k] *b[k][j];
printf("Enter element b%d%d: ",i+1,j+1); }
scanf("%d",&b[i][j]); }
} }
} printf("Resultant matrix:\n");
printf("Elements of matrix1\n"); for (i=0; i<r1; i++)
for (i=0; i<r1; i++) {
{ for (j=0; j<c2; j++)
for (j=0; j<c1; j++) {
{ printf("%d\t", mult[i][j]);
printf("%d\t", a[i][j]); }
} printf("\n");
printf("\n"); }
} return 0;
printf("Elements of matrix2\n"); }
for (i=0; i<r2; i++)
{
for (j=0; j<c2; j++)
{
ST THOMAS COLLEGE OF ENGINEERING & TECHNOLOGY KOZHUVALLOOR 12
Output:
Enter rows and columns for first matrix: 3 2
Enter rows and columns for second matrix: 2 3
Enter elements of matrix 1:
Enter element a11: 1
Enter element a12: 2
Enter element a21: 3
Enter element a22: 4
Enter element a31: 5
Enter element a32: 6
Enter elements of matrix 2:
Enter element b11: 7
Enter element b12: 8
Enter element b13: 9
Enter element b21: 1
Enter element b22: 2
Enter element b23: 3
Elements of matrix1
1 2
3 4
5 6
Elements of matrix2
7 8 9
1 2 3
Resultant matrix:
9 12 15
25 32 39
41 52 63
ST THOMAS COLLEGE OF ENGINEERING & TECHNOLOGY KOZHUVALLOOR 13