Basic
Practice Problems based on chapter which you learn in previous Tutorials.
1. Largest Element in Array
#include <stdio.h>
Copy Code
int
main
() {
int
size;
printf
("Enter the size of the array: ");
scanf
("%d", &size);
int
arr[size];
printf
("Enter %d elements:
", size);
for
(
int
i = 0; i < size; i++) {
scanf
("%d", &arr[i]);
}
int
largest = arr[0];
for
(
int
i = 1; i < size; i++) {
if
(arr[i] > largest) {
largest
= arr[i];
}
}
printf
("The largest element in the array is: %d
", largest);
return
0;
}
2. Addition of two Matrics
#include <stdio.h>
Copy Code
int
main
() {
int
rows, columns;
printf
("Enter the number of rows: ");
scanf
("%d", &rows);
printf
("Enter the number of columns: ");
scanf
("%d", &columns);
printf
("Enter elements of the first matrix:
");
int
matrix1[rows][columns];
for
(
int
i = 0; i < rows; i++) {
for
(
int
j = 0; j < columns; j++) {
scanf
("%d", &matrix1[i][j]);
}
}
printf
("Enter elements of the second matrix:
");
int
matrix2[rows][columns];
for
(
int
i = 0; i < rows; i++) {
for
(
int
j = 0; j < columns; j++) {
scanf
("%d", &matrix2[i][j]);
}
}
int
sumMatrix[rows][columns];
for
(
int
i = 0; i < rows; i++) {
for
(
int
j = 0; j < columns; j++) {
sumMatrix
[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf
("Sum of the matrices:
");
for
(
int
i = 0; i < rows; i++) {
for
(
int
j = 0; j < columns; j++) {
printf
("%d ",
sumMatrix
[i][j]);
}
printf
("
");
}
return
0;
}
3. Multiply two Matrics
#include <stdio.h>
Copy Code
int
main
() {
int
firstMatrix[MAX_SIZE][MAX_SIZE], secondMatrix[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
int
rowFirst, colFirst, rowSecond, colSecond;
printf
("Enter the number of rows and columns of the first matrix: ");
scanf
("%d %d", &rowFirst, &colFirst);
printf
("Enter the elements of the first matrix:
");
for
(
int
i = 0; i < rowFirst; i++) {
for
(
int
j = 0; j < colFirst; j++) {
scanf
("%d", &firstMatrix[i][j]);
}
}
printf
("Enter the number of rows and columns of the second matrix: ");
scanf
("%d %d", &rowSecond, &colSecond);
printf
("Enter the elements of the second matrix:
");
for
(
int
i = 0; i < rowSecond; i++) {
for
(
int
j = 0; j < colSecond; j++) {
scanf
("%d", &secondMatrix[i][j]);
}
}
if
(colFirst != rowSecond) {
printf
("Matrices cannot be multiplied.
");
return
1;
}
for
(
int
i = 0; i < rowFirst; i++) {
for
(
int
j = 0; j < colSecond; j++) {
result
[i][j] = 0;
for
(
int
k = 0; k < colFirst; k++) {
result
[i][j] +=
firstMatrix
[i][k] *
secondMatrix
[k][j];
}
}
}
printf
("Product of the matrices:
");
for
(
int
i = 0; i < rowFirst; i++) {
for
(
int
j = 0; j < colSecond; j++) {
printf
("%d ",
result
[i][j]);
}
printf
("
");
}
return
0;
}
4. Length of string
#include <stdio.h>
Copy Code
int
main
() {
char
str[100];
printf
("Enter a string: ");
scanf
("%s", str);
int
length =
strlen
(str);
printf
("Length of the string: %d
", length);
return
0;
}
5. Concatination of string
The concatenation operators combine two strings to form one string by appending the second string to the right-hand end of the first string.
#include <stdio.h>
Copy Code
int
main
() {
char
str1[100], str2[100];
printf
("Enter the first string: ");
scanf
("%s", str1);
printf
("Enter the second string: ");
scanf
("%s", str2);
strcat
(str1, str2);
printf
("Concatenated string: %s
", str1);
return
0;
}