Ex 2 - DSCP Lab
Ex 2 - DSCP Lab
AIM:
Write a C program to find the factorial of a number using recursion.
ALGORITHM:
1. Start the program.
2. Read the value of num.
3. Call Fact (num).
4. Print the result.
5. Stop the program.
PROGRAM:
/* Find the factorial of a number using recursion */
#include <stdio.h>
#include <conio.h>
int Fact (int);
int main ()
{
int num, factorial;
clrscr ();
printf (“\n Enter the number: ”);
scanf (“%d”, &num);
factorial = Fact (num);
printf (“\n Factorial of %d = %d”, num, factorial);
getch ();
return 0;
}
int Fact (int n)
{
if (n == 1)
return 1;
else
return (n* Fact (n-1));
}
OUTPUT:
Enter the number: 5
Factorial of 5 = 120
RESULT:
Thus, the implementation of finding the factorial of a number using
recursion was executed successfully.
1
EX. NO : 2B MATRIX ADDITION
DATE :
AIM:
To write a C program to perform the matrix addition.
ALGORITHM:
1. Start the program.
2. Read the first matrix.
3. Read the second matrix.
4. Perform addition.
5. Stop the program.
PROGRAM
/* Perform the matrix addition */
#include <stdio.h>
#include <conio.h>
int main ()
{
int a [3][3], b [3][3], c [3][3], i, j;
clrscr ();
printf (“Enter the first matrix:\n”);
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf (“%d”, &a [i][j]);
}
}
printf (“Enter the second matrix:\n”);
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf (“%d”, &b [i][j]);
}
}
printf (“Addition of two matrices:\n”);
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
c [i][j] = a [i][j] + b [i][j];
printf (“%d ”, c [i][j]);
}
printf (“\n”);
2
}
getch ();
return 0;
}
OUTPUT:
Enter the first matrix:
111
111
111
Enter the second matrix:
222
222
222
Addition of two matrices:
333
333
333
RESULT:
Thus, the implementation of matrix addition was executed successfully.
3
EX. NO : 2C MATRIX MULTIPLICATION
DATE :
AIM:
To write a C program to perform the matrix multiplication.
ALGORITHM:
1. Start the program.
2. Read the first matrix.
3. Read the second matrix.
4. Perform multiplication.
5. Stop the program.
PROGRAM:
/* Perform the matrix multiplication */
#include <stdio.h>
#include <conio.h>
int main ()
{
int m1 [10][10], m2 [10][10], m3 [10][10];
int m1r, m1c, m2r, m2c, i, j, k;
clrscr ();
printf (“Enter the first matrix row and column:\n”);
scanf (“%d %d”, &m1r, &m1c);
printf (“Enter the second matrix row and column:\n”);
scanf (“%d %d”, &m2r, &m2c);
if (m1c = m2r)
{
printf (“Enter the first matrix elements:\n”);
for (i = 0; i < m1r; i++)
{
for (j = 0; j < m1c; j++)
{
scanf (“%d”, &m1 [i][j]);
}
}
printf (“Enter the second matrix elements:\n”);
for (i = 0; i < m2r; i++)
{
for (j = 0; j < m2c; j++)
{
scanf (“%d”, &m2 [i][j]);
}
}
printf (“Multiplication of two matrices:\n”);
for (i = 0; i < m1r; i++)
{
4
for (j = 0; j < m2c; j++)
{
m3 [i][j] = 0;
for (k = 0; k < m2r; k++)
{
m3 [i][j] = (m1 [i][k] * m2 [k][j]) + m3 [i][j];
}
printf (“\t%d”, m3[i][j]);
}
printf (“\n”);
}
}
else
{
printf (“Enter correct row and column:\n”);
}
getch ();
return 0;
}
OUTPUT:
Enter the first matrix row and column:
33
Enter the second matrix row and column:
33
Enter the first matrix elements:
121
312
231
Enter the second matrix elements:
231
121
312
Multiplication of two matrices:
7 8 5
13 13 8
10 13 7
RESULT:
Thus, the implementation of matrix multiplication was executed
successfully.