program to perform the addition of two matrices
program to perform the addition of two matrices
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0; }
What is Recursion in C?
Recursion is a programming technique where a function calls itself to solve a problem. It is commonly used to break a complex problem into smaller
, more manageable parts. A recursive function typically has two main components:
Base Case: A condition under which the function stops calling itself to prevent infinite loops.
Recursive Case: The part of the function that includes the self-call, where the problem is broken down into smaller subproblems.
#include <stdio.h>
// Recursive function to calculate factorial
int factorial(int n) {
// Base case
if (n == 0 || n == 1) {
return 1;
}
// Recursive case
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num)); // Outputs: 120
return 0;
}
a string library function refers to a set of built-in functions that help you work with strings.
Strings in C are essentially arrays of characters, and these functions make it easier to manipulate them without having to
write complex code from scratch.
The string library in C is included through the header file <string.h>.
strlen: Calculates the length of a string (number of characters)
char str[] = "Hello";
int length = strlen(str); // length will be 5
Defining an Array:
Definition: Defining an array is the process of declaring an array's name, type, and size in a programming
language. This creates a storage structure that can hold multiple values of the same data type, allowing the
program to manage and access these values efficiently.
Initializing an Array:
Definition: Initializing an array is the process of assigning specific values to its elements at the time of
definition. This sets the initial state of the array, allowing it to be used immediately with predefined values.