Task 8
Task 8
Program:
include <stdio.h>
int main( )
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
}
printf("\n");
}
return 0;
}
Output:
Enter elements of 1st matrix:
Enter element a11: 1
Enter element a12: 2
Enter element a13: 3
Enter element a21: 4
Enter element a22: 5
Enter element a23: 6
Enter element a31: 7
Enter element a32: 8
Enter element a33: 9
Enter elements of 2nd matrix:
Enter element b11: 9
Enter element b12: 8
Enter element b13: 7
Enter element b21: 6
Enter element b22: 5
Enter element b23: 4
Enter element b31: 3
Enter element b32: 2
Enter element b33: 1
We can add, subtract, multiply and divide 2 matrices. To do so, we are taking input from
the user for row number, column number, first matrix elements and second matrix
elements. Then we are performing multiplication on the matrices entered by the user.
In matrix multiplication first matrix one row element is multiplied by second matrix all
column elements.
Let's try to understand the matrix multiplication of 2*2 and 2*3 matrices by the figure
given below:
Program:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
1
2
3
4
5
6
1
2
3
enter the second matrix element=
1
2
3
4
5
6
1
2
3
multiply of the matrix=
12 18 24
30 45 60
12 18 24
Result: The given program to multiply two matrices has been executed successfully,
iii) Sort array elements using bubble sort
Aim: Write a C program to sort array of elements using Bubble sort
Description:
Bubble sort works on the repeatedly swapping of adjacent elements until they are not in
the intended order. It is called bubble sort because the movement of array elements is
just like the movement of air bubbles in the water. Bubbles in water rise up to the
surface; similarly, the array elements in bubble sort move to the end in each iteration.
Program:
#include<stdio.h>
int main( )
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
return 0;
}
Output:
Enter First String:shali
Result: The given program to Concatenate two strings without built functions has been
executed successfully.
v) Reverse a string using built-in string functions
Aim: Write a C program to reverse a string using built-in string functions.
Description:
Reversing a string is the technique that reverses or changes the order of a given string
so that the last character of the string becomes the first character of the string.
We have built in function strrev( ) to reverse a string.
Syntax: strrev(stringvariable)
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char str[40]; // declare the size of character string
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);
//use gets(str) if you want to reverse a line
// use strrev() function to reverse a string
printf (" \n After the reverse of a string: %s ", strrev(str));
return 0;
}
Output:
Enter a string to be reversed: ksrmce
Program:
include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char string[20],temp;
int i,length;
printf("Enter String : ");
scanf("%s",string);
length=strlen(string)-1;
for(i=0;i<strlen(string)/2;i++)
{
temp=string[i];
string[i]=string[length];
string[length--]=temp;
}
printf("Reverse string :%s",string);
getch();
}
Output:
Enter String : firstyearstudents
Reverse string :stnedutsraeytsrif
Result: The given program to reverse a string without built-in string functions ahs
been executed successfully.
vi) String palindrome or not
Aim: Write a C program to find out given string is palindrome or not.
Description:
To find given string is palindrome or not, we have to do reverse a string . if the reversed
string and given string are same then it will called as Palindrome otherwise not.
Program:
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int i, len, temp=0;
int flag = 0;
printf("Enter a string:");
scanf("%s", str);
len = strlen(str);
for(i=0; i < len ; i++)
{
if(str[i] != str[len-i-1])
{
temp = 1;
break;
}
}
if (temp==0)
{
printf("String is a palindrome");
}
else
{
printf("String is not a palindrome");
}
return 0;
}
Output:
Enter a string:ksrmmce
String is not a palindrome
Enter a string:madam
String is a palindrome
--------------------------------
Result: The given Program to find given string is palindrome or not has been executed
succesfullly.