Ayush Joshi Sapid-500122661

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Ans1-#include <stdio.

h>

int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int main_diagonal_product = 1;
int secondary_diagonal_product = 1;
for (int i = 0; i < 3; i++) {
main_diagonal_product *= matrix[i][i];
}
for (int i = 0; i < 3; i++) {
secondary_diagonal_product *= matrix[i][2 - i];
}

int result = main_diagonal_product * secondary_diagonal_product;

printf("Multiplication of the diagonals: %d\n", result);

return 0;

}
Ans2-#include <stdio.h>
int main() {
int n; // Size of the square matrix
printf("Enter the size of the square matrix: ");
scanf("%d", &n);

if (n <= 0) {
printf("Invalid matrix size.\n");
return 1;
}
int matrix[n][n];
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
int sum = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
sum += matrix[i][j];
}
}
printf("Sum of elements in the upper right side triangle: %d\n", sum);

return 0;}
Ans3-#include <stdio.h>
int main() {
int m, n; // Dimensions of the matrix
printf("Enter the number of rows: ");
scanf("%d", &m);
printf("Enter the number of columns: ");
scanf("%d", &n);
if (m <= 0 || n <= 0) {
printf("Invalid matrix dimensions.\n");
return 1;
}
int matrix[m][n], transpose[n][m];
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
} for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
transpose[j][i] = matrix[i][j];
}}
printf("Transpose of the matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}

return 0;
}

Ans4-#include <stdio.h>
int main() {
int m, n;
printf("Enter a number for m: ");
scanf("%d", &m);
printf("Enter a number for n: ");
scanf("%d", &n);
double result = (double)m / n;
char result_str[100];
sprintf(result_str, "%.6lf", result);
int digits[100];
int num_digits = 0;
for (int i = 0; result_str[i] != '\0'; i++) {
if (result_str[i] >= '0' && result_str[i] <= '9') {
digits[num_digits++] = result_str[i] - '0';
}
}
int sum_of_digits = 0;
for (int i = 0; i < num_digits; i++) {
sum_of_digits += digits[i];
}
printf("Sum of the digits of m/n: %d\n", sum_of_digits);
return 0;
}

Ans5-#include <stdio.h>

int main() {
int m, n;
printf("Enter the number of rows: ");
scanf("%d", &m);
printf("Enter the number of columns: ");
scanf("%d", &n);
if (m <= 0 || n <= 0) {
printf("Invalid matrix dimensions.\n");
return 1;
}
int matrix1[m][n], matrix2[m][n], result[m][n];
printf("Enter the elements of the first matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter the elements of the second matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix2[i][j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf("Resultant Matrix (Sum of matrices):\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

return 0;
}

Ans6-#include <stdio.h>

int main() {
char charArray[10];
char searchChar;
int count = 0;
printf("Enter a string of characters (up to 10 characters): ");
scanf("%s", charArray);
printf("Enter the character to count: ");
scanf(" %c", &searchChar);
for (int i = 0; i < 10; i++) {
if (charArray[i] == searchChar) {
count++;
}
}
printf("The character '%c' occurs %d times in the array.\n", searchChar, count);

return 0;
}
Ans7-#include <stdio.h>
int main() {
char charArray[10];
char searchChar;
int count = 0;
printf("Enter a string of characters (up to 10 characters): ");
scanf("%s", charArray);
printf("Enter the character to count: ");
scanf(" %c", &searchChar);
for (int i = 0; i < 10; i++) {
if (charArray[i] == searchChar) {
count++;
}
}
printf("The character '%c' occurs %d times in the array.\n", searchChar, count);

return 0;
}
Ans8-#include <stdio.h>
int main() {
int rows1, cols1, rows2, cols2;
printf("Enter the number of rows for the first matrix: ");
scanf("%d", &rows1);
printf("Enter the number of columns for the first matrix: ");
scanf("%d", &cols1);
printf("Enter the number of rows for the second matrix: ");
scanf("%d", &rows2);
printf("Enter the number of columns for the second matrix: ");
scanf("%d", &cols2);
if (cols1 != rows2) {
printf("Matrix multiplication is not possible. Number of columns in the first matrix must
be equal to the number of rows in the second matrix.\n");
return 1;
}
int matrix1[rows1][cols1];
int matrix2[rows2][cols2];
int result[rows1][cols2];
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
scanf("%d", &matrix2[i][j]);
}
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
printf("Resultant matrix after multiplication:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
Ans9-#include <stdio.h>
#include <string.h>
int main() {
char str[1000], longestWord[1000] = "", word[1000];
int maxLength = 0;
printf("Enter a string: ");
gets(str);
char *token = strtok(str, " \t\n\r");
while (token != NULL) {
if (strlen(token) > maxLength) {
maxLength = strlen(token);
strcpy(longestWord, token);
}
token = strtok(NULL, " \t\n\r");
}
printf("Longest word in the string: %s\n", longestWord);

return 0;

Ans10-#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
bool isWhitespace(char c) {
return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
}
int main() {
char str[1000];
int wordCount = 0;
bool inWord = false;
printf("Enter a string: ");
gets(str);
for (int i = 0; str[i] != '\0'; i++) {
if (isWhitespace(str[i])) {
inWord = false;
} else if (!inWord) {
wordCount++;
inWord = true;
}
}
printf("Number of words in the string: %d\n", wordCount);

return 0;
}
Ans11-#include <stdio.h>
#include <string.h>
int main() {
char strings[4][100];
int lengths[4];
for (int i = 0; i < 4; i++) {
printf("Enter string %d: ", i + 1);
gets(strings[i]);
lengths[i] = strlen(strings[i]);
}
int equalLengths = 1;
for (int i = 1; i < 4; i++) {
if (lengths[i] != lengths[0]) {
equalLengths = 0;
break;
}
}
if (equalLengths) {
int equivalent = 1;
for (int i = 1; i < 4; i++) {
if (strcmp(strings[i], strings[0]) != 0) {
equivalent = 0;
break;
}
}
if (equivalent) {
printf("The strings are of the same length and equivalent.\n");
} else {
printf("The strings are of the same length but not equivalent.\n");
}
} else {
printf("The strings are not of the same length.\n");
}
return 0;
}

Ans12-#include <stdio.h>
#include <string.h>
int main() {
char strings[4][100];
int lengths[4
for (int i = 0; i < 4; i++) {
printf("Enter string %d: ", i + 1);
gets(strings[i]);
lengths[i] = strlen(strings[i]);
}
int equalLengths = 1;
for (int i = 1; i < 4; i++) {
if (lengths[i] != lengths[0]) {
equalLengths = 0;
break;
}
}
if (equalLengths) {

int equivalent = 1;
for (int i = 1; i < 4; i++) {
if (strcmp(strings[i], strings[0]) != 0) {
equivalent = 0;
break;
}
}
if (equivalent) {
printf("The strings are of the same length and equivalent.\n");
} else {
printf("The strings are of the same length but not equivalent.\n");
}
} else {
printf("The strings are not of the same length.\n");
}
return 0;
}

You might also like