Exno.5.
User defined Functions
Date:25.11.2021
Program 19
Aim-
Sum of Two matrix
ALGORITHM :
STEP 1 : START
STEP 2 : MATRIX INPUT
STEP3 : Addition OF MATRIC
STEP 4 : PRINT YOUR OUTPUT
Code-
#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 : ");
scanf("%d", &r);
printf("Enter the number of columns : ");
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]);
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
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;
}
Output-
Result –
Thus the program is executed successfully.
Program B
Aim-
Factorial using recursive functions
ALGORITHM :
STEP 1 : START
STEP 2 : DEFINE n
STEP3 : GET THE NUMBER FROM THE USER
STEP 4 : USE THE OPERATORS TO FIND THE FACTORIAL OF THE GIVEN
NUMBER
STEP 5 : PRINT YOUR OUTPUT
Code
#include <stdio.h>
int fact(int);
int main()
{
int n;
printf("enter number:");
scanf("%d",&n);
printf("factorial of %d =%d",n,fact(n));
}
int fact(int n)
{
if(n!=1)
n=n*fact(n-1);
return n;
}
Output-
Result –
Thus the program is executed successfully.