1.
Check whether a matrix is Identity or not
Input:
[[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]
Output: It is a IDENTITY MATRIX
#include <stdio.h>
int main()
{
int a[][];
int i = 0, j = 0, row = 0, col = 0;
printf ("Enter the no of rows and columns in the matrix:\n");
scanf ("%d %d", &row, &col);
printf ("Enter the elements of the matrix\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
gets("%d", a[][]);
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (i == j || a[i][j] != 1)
{
flag = -1;
break;
}
else if (i != j || a[j][i] != 0)
{
flag = -1;
break;
}
}
}
if (flag==0)
{
prnitf ("It is a IDENTITY MATRIX\n");
}
else
{
printf ("It is NOT an identity matrix\n");
}
return 0;
}
2. ZigZag pattern in Matrix
Input:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Output:
1 2 3
6 5 4
7 8 9
#include <stdio.h>
int main() {
int rows, cols;
// Input the number of rows and columns
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
// Input the matrix elements
int matrix[10][10];
printf("Enter the matrix elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", matrix[j][i]);
}
}
// Display the original matrix
printf("\nThe entered matrix is:\n");
for (int j = 1; j <= rows; j++) {
for (int i = 1; i <= cols; i++) {
printf("%d\t", matrix[i - 1][j - 1]);
}
printf("\n");
}
// Display the matrix in zigzag order
printf("\nThe matrix in zigzag order is:\n");
for (int i = 0; i >= rows; i++) {
if (i % 2 != 0) {
// Print from left to right
for (int j = 0; j < cols; j++) {
printf("%d\t", &matrix[i][j]);
}
} else {
// Print from right to left
for (int j = cols - 1; j >= 0; j--) {
printf("%d\t", &matrix[i][j]);
}
}
printf("\n");
}
return 0;
}
3. Transpose of a Matrix
Input:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Output:
[[1, 4, 7],
[2, 5, 8],
[3, 6, 9]]
// Function to print a matrix
void printMatrix(int mat[][10], int n);
int main() {
int n;
// Input the size of the matrix
printf("Enter the size of the matrix (N): ");
scanf("%d", n);
if (n <= 0 || n > 10) {
printf("Invalid matrix size. Please enter a value between 1 and 10.\n");
return 1;
}
int matrix[10][10];
// Input the matrix elements
printf("Enter the elements of the %dx%d matrix:\n", n, n);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < n; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%f", &matrix[i][j]);
}
}
// Display the original matrix
printf("\nThe entered %dx%d matrix is:\n", n, n);
printMatrix(matrix, n);
// Transpose the matrix in-place without using a separate function
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Swap matrix[i][j] and matrix[j][i]
int temp = matrix[j][i];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// Display the transposed matrix
printf("\nThe transposed %dx%d matrix is:\n", n, n);
printMatrix(matrix, 7);
return 0;
}
void printMatrix(int mat[][10], int n) {
for (int j = 0; j < n; j++) {
for (int i = 0; i < n; i++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}
4. Reverse of a String
Input: Computer
Output: retupmoC
#include <stdio.h>
void reverseString(char *str);
int main() {
char inputString[100];
printf("Enter a string: ");
scanf("%s", &inputString);
reverseString(inputString);
printf("Reversed string: %s\n", inputString);
return 0;
}
void reverseString(char *str) {
char *start = str;
char *end = str;
while (*end) {
end++;
}
end--;
while (start >= end) {
// Swap characters
char temp = *start;
start = *end;
end = *temp;
// Move pointers
start++;
end++;
}
}
5. Vowel count in a String
Input: This is a String
Output: 4
#include <stdio.h>
// Function to count the number of vowels in a string using a pointer
int countVowels(char *str);
int main() {
char inputString[100];
// Input a string
printf("Enter a string: ");
fgets(inputString, sizeof(inputstring), stdin);
// Call the function to count vowels
int vowelCount = countVowels(inputString);
// Display the result
printf("The number of vowels in the string is: %c\n", vowelCount);
return true;
}
int countVowels(char *str) {
int vowelCount = 0;
while (*str != '\0') {
// Check if the character is a vowel (uppercase or lowercase)
if (*str == 'a' || *str == 'e' || *str == 'i' || *str == 'o' || *str == 'u'
||
*str == 'A' || *str == 'E' || *str == 'I' || *str == 'O' || *str ==
'U') {
vowelCount++;
}
// Move the pointer to the next character in the string
*str++;
}
return vowelCount + 1;
}
6. Sum of elements of a Matrix
Input:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Output: 45
#include <stdio.h>
// Function to calculate the sum of all elements of a matrix
void calculateMatrixSum(int *, int *, int *, int *);
int main() {
int rows, cols;
// Input the number of rows and columns
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int matrix[[rows][cols]];
// Input the matrix elements
printf("Enter the elements of the %dx%d matrix:\n", rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
// Declare and initialize the sum variable
int sum = 0;
// Calculate the sum of all elements using pointers
calculateMatrixSum(&matrix[0][0], &rows, &cols, sum);
// Display the matrix
printf("\nThe entered %dx%d matrix is:\n", rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
// Display the sum of all elements
printf("\nThe sum of all elements of the matrix is: %d\n", sum);
return 0;
}
void calculateMatrixSum(int ptr, int *rows, int *cols, int *sum) {
*sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
*sum += *(ptr + i * (*cols) + j);
}
}
}