C Programming Examples: Matrices, Strings, and Structures
C Program for Addition of Two 2×2 Matrices
Code:
#include <stdio.h>
int main() {
int a[2][2], b[2][2], c[2][2];
int i, j;
// Get the elements of the first matrix
printf(“Enter the elements of matrix A:\n”);
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf(“%d”, &a[i][j]);
}
}
// Get the elements of the second matrix
printf(“Enter the elements of matrix B:\n”);
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf(“%d”, &b[i][j]);
}
}
// Add the two matrices
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
// Print the result matrix
printf(“Result of adding two matrices:\n”);
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf(“%d\t”, c[i][j]);
}
printf(“\n”);
}
return 0;
}
C Program for Subtraction of 2×2 Matrices
Code:
#include <stdio.h>
int main() {
int a[2][2], b[2][2], c[2][2], i, j;
printf(“Enter the elements of the first matrix:\n”);
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf(“%d”, &a[i][j]);
}
}
printf(“Enter the elements of the second matrix:\n”);
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf(“%d”, &b[i][j]);
}
}
printf(“Subtraction of two matrices:-\n”);
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
c[i][j] = a[i][j] – b[i][j];
printf(“%d\t”, c[i][j]);
}
printf(“\n”);
}
return 0;
}
C Program for Multiplication of 2×2 Matrices
Code:
#include <stdio.h>
int main() {
int a[2][2], b[2][2], c[2][2];
int i, j, k;
// Get the elements of the first matrix
printf(“Enter the elements of the first matrix:\n”);
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf(“%d”, &a[i][j]);
}
}
// Get the elements of the second matrix
printf(“Enter the elements of the second matrix:\n”);
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf(“%d”, &b[i][j]);
}
}
// Multiply the two matrices
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
c[i][j] = 0;
for (k = 0; k < 2; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
// Print the resultant matrix
printf(“The resultant matrix is:\n”);
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf(“%d “, c[i][j]);
}
printf(“\n”);
}
return 0;
}
C++ Program to Demonstrate String Functions
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = “Hello”;
string str2 = “World”;
// Compare the length of two strings
cout << “The length of str1 is: ” << str1.length() << endl;
cout << “The length of str2 is: ” << str2.length() << endl;
// Copy str1 to str3
string str3 = str1;
cout << “The value of str3 is: ” << str3 << endl;
// Concatenate str1 and str2
string str4 = str1 + str2;
cout << “The value of str4 is: ” << str4 << endl;
// Reverse str1
string str5 = str1;
reverse(str5.begin(), str5.end());
cout << “The value of str5 is: ” << str5 << endl;
return 0;
}
C Program to Manage Book Details Using Structures
Code:
#include <stdio.h>
// Define the structure for Book
struct Book {
int Id;
char Title[100];
char Author[100];
};
int main() {
// Declare an array of structures to store details of 5 books
struct Book books[5];
// Input details for 5 books
printf(“Enter details for 5 books:\n”);
for (int 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);
printf(“Enter Author: “);
scanf(” %[^ ]s”, books[i].Author);
}
// Display details of all 5 books
printf(“\nDetails of the 5 books:\n”);
for (int 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 to Manage Student Information Using Structures
Code:
#include <stdio.h>
struct student {
char name[50];
int roll;
float marks;
};
int main() {
struct student s[5];
// Read information of students from user
for (int i = 0; i < 5; i++) {
printf(“Enter information of student %d:\n”, i + 1);
printf(“Enter name: “);
scanf(“%s”, s[i].name);
printf(“Enter roll number: “);
scanf(“%d”, &s[i].roll);
printf(“Enter marks: “);
scanf(“%f”, &s[i].marks);
}
// Display information of students
printf(“\nStudent information:\n”);
for (int i = 0; i < 5; i++) {
printf(“Student %d:\n”, i + 1);
printf(“Name: %s\n”, s[i].name);
printf(“Roll number: %d\n”, s[i].roll);
printf(“Marks: %.2f\n”, s[i].marks);
}
return 0;
}
Using Typedef with Structures in C
Typedef allows you to create aliases for data types in C, making your code more concise and readable. Here’s how it’s used with structures:
Code:
#include <stdio.h>
typedef struct {
int x;
int y;
} Point;
int main() {
Point p1;
p1.x = 10;
p1.y = 20;
printf(“The point is (%d, %d)\n”, p1.x, p1.y);
return 0;
}
In this example, typedef creates an alias Point for the structure. Now, you can declare variables of type Point directly, without writing struct every time.
