C Programming Examples: Matrices, Strings, and Structures
Posted on May 22, 2024 in Computers
C Program for 2×2 Matrix Addition
Code:
#include <stdio.h>
int main() {
int matrix1[2][2] = {{1, 2}, {3, 4}};
int matrix2[2][2] = {{5, 6}, {7, 8}};
int result[2][2];
int i, j;
// Adding matrices
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Displaying the result
printf("Sum of matrices:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
C Program for 2×2 Matrix Subtraction
Code:
#include <stdio.h>
int main() {
int matrix1[2][2] = {{1, 2}, {3, 4}};
int matrix2[2][2] = {{5, 6}, {7, 8}};
int result[2][2];
int i, j;
// Subtracting matrices
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
// Displaying the result
printf("Difference of matrices:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
C Program for 2×2 Matrix Multiplication
Code:
#include <stdio.h>
int main() {
int matrix1[2][2] = {{1, 2}, {3, 4}};
int matrix2[2][2] = {{5, 6}, {7, 8}};
int result[2][2];
int i, j, k;
// Multiplying matrices
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
result[i][j] = 0;
for (k = 0; k < 2; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Displaying the result
printf("Product of matrices:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
C Program: String Functions (Compare, Length, Copy, Concatenate, Reverse)
Code:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], copy_str[100], concat_str[200], rev_str[100];
// Input strings
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
// Compare strings
int cmp = strcmp(str1, str2);
if (cmp == 0) {
printf("Strings are equal.\n");
} else if (cmp < 0) {
printf("String \"%s\" is less than string \"%s\".\n", str1, str2);
} else {
printf("String \"%s\" is greater than string \"%s\".\n", str1, str2);
}
// Length of strings
int len_str1 = strlen(str1);
int len_str2 = strlen(str2);
printf("Length of string 1: %d\n", len_str1);
printf("Length of string 2: %d\n", len_str2);
// Copy strings
strcpy(copy_str, str1);
printf("Copied string: %s\n", copy_str);
// Concatenate strings
strcpy(concat_str, str1);
strcat(concat_str, str2);
printf("Concatenated string: %s\n", concat_str);
// Reverse strings
int i, j;
for (i = 0, j = len_str1 - 1; i < len_str1; i++, j--) {
rev_str[i] = str1[j];
}
rev_str[i] = '\0';
printf("Reversed string 1: %s\n", rev_str);
return 0;
}
C Program: Structure for Book Details (Using Array of Structures)
Code:
#include <stdio.h>
// Define the structure for a book
struct Book {
int id;
char title[100];
char author[100];
};
int main() {
struct Book books[5]; // Array of structures to store 5 books
int i;
// Input details for 5 books
printf("Enter details for 5 books:\n");
for (i = 0; i < 5; i++) {
printf("\nBook %d:\n", i + 1);
printf("Enter ID: ");
scanf("%d", &books[i].id);
printf("Enter Title: ");
scanf("%s", books[i].title); // Assuming no spaces in title
printf("Enter Author: ");
scanf("%s", books[i].author); // Assuming no spaces in author
}
// Display details of all 5 books
printf("\nDetails of 5 books:\n");
for (i = 0; i < 5; i++) {
printf("\nBook %d:\n", i + 1);
printf("ID: %d\n", books[i].id);
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
}
return 0;
}
C Program: Structure for Student Information
Code:
#include <stdio.h>
// Define the structure for student information
struct Student {
char name[100];
int roll_no;
float marks;
};
int main() {
struct Student student;
// Input details from the user
printf("Enter student details:\n");
printf("Name: ");
scanf("%s", student.name);
printf("Roll No: ");
scanf("%d", &student.roll_no);
printf("Marks: ");
scanf("%f", &student.marks);
// Display the student details
printf("\nStudent Details:\n");
printf("Name: %s\n", student.name);
printf("Roll No: %d\n", student.roll_no);
printf("Marks: %.2f\n", student.marks);
return 0;
}