0% found this document useful (0 votes)
17 views

program to perform the addition of two matrices

Uploaded by

aashish
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

program to perform the addition of two matrices

Uploaded by

aashish
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

//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

strcpy: Copies one string to another.


char source[] = "Hello";
char destination[10];
strcpy(destination, source); // destination now holds "Hello"

strcat: Concatenates (joins) two strings


char str1[20] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2); // str1 now holds "Hello, World!"

strcmp: Compares two strings to see if they are equal.


char str1[] = "Hello";
char str2[] = "Hello";
int result = strcmp(str1, str2); // result will be 0 (equal)
Control statements in programming are instructions that determine the flow of control in a program. They allow you to make
decisions, repeat actions, and control how the program executes based on certain conditions. In C, control statements help you
manage the execution path of your code.
Decision-Making Statements- help your program make choices based on certain conditions. Think of them as
a way to ask,"If this is true, do this; otherwise, do that."
Looping Statements allow you to repeat a block of code multiple times. It’s like saying, "Keep doing this
until a certain condition is met."
Jump Statements allow you to jump to a different part of your code, changing the normal flow of
execution. They are like shortcuts in your program.

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.

You might also like