0% found this document useful (0 votes)
18 views2 pages

5.user Defined Functions

The document outlines two programs: one for summing two matrices and another for calculating the factorial of a number using recursive functions. Each program includes an algorithm, C code, and confirms successful execution. The matrix program prompts for matrix dimensions and elements, while the factorial program prompts for a number and calculates its factorial recursively.

Uploaded by

siddharthk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

5.user Defined Functions

The document outlines two programs: one for summing two matrices and another for calculating the factorial of a number using recursive functions. Each program includes an algorithm, C code, and confirms successful execution. The matrix program prompts for matrix dimensions and elements, while the factorial program prompts for a number and calculates its factorial recursively.

Uploaded by

siddharthk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

You might also like