EXP2
EXP2
NO - 2(A)
DATE : FACTORIAL USING RECURSION
AIM :
To write a C program on factorial of a number using recursion .
ALGORITHM :
STEP 3: if n<0 then print the factorial is not defined otherwise go to step 4.
STEP 4: call the function factorial where the factorial value is compared using the
recursive function call n*factorial(n-1).
#include<stdio.h>
#include<conio.h>
long factorial(int);
int main()
{
int n;
long f;
printf("Enter an integer to find its factorial\n");
scanf("%d", &n);
if (n < 0)
printf("Factorial of negative integers isn't defined.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n,
f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
SAMPLE OUTPUT :
Enter a number 5
Factorial of 5 is 120
RESULT :
Thus , the C program to calculate factorial of a number using recursion has been executed
successfully .
EX. NO - 2(B)
DATE : MATRIX ADDITION
AIM:
To write a C program on matrix addition .
ALGORITHM :
STEP 1: Start.
STEP 3: Enter the number of rows & columns required for a matrix.
STEP 7: Stop.
PROGRAM :
#include <stdio.h>
#include<conio.h>
int main()
{
int i, b, c, d;
int m1[10][10], m2[10][10], sum[10][10];
printf("Please enter the number of rows of matrix\n");
scanf("%d", &a);
printf("Please enter number of columns of matrix\n");
scanf("%d", &b);
printf("Please enter the elements of first matrix one by one\n");
for ( i = 0 ; i < a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
scanf("%d", &m1[i][j]);
}
}
printf("Please enter the elements of second matrix one by one\n");
for ( i = 0 ; i< a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
scanf("%d", &m2[i][j]);
}
}
for ( i = 0 ; i < a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
sum[i][j] = m1[i][j] + m2[i][j];
}
}
printf("The sum of entered matrices is below:\n");
for ( i = 0 ; i < a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
printf("%d\t", sum[i][j]);
printf("\n");
}
getch();
}
SAMPLE OUTPUT :
RESULT :
Thus , the C program to perform matrix addition has been executed successfully .
EX. NO - 2(C)
DATE : MATRIX MULTIPLICATION
AIM :
To write a C program on matrix multiplication .
ALGORITHM :
STEP 1: Start.
STEP 3: Enter the number of rows & columns required for a matrix.
STEP 7: Stop.
PROGRAM :
#include<stdio.h>
main()
{
int i,j,k,rows_1,rows_2,col_1,col_2,sum=0;
printf("Enter number of rows and columns of matrix 1\n");
scanf("%d %d",&rows_1,&col_1);
printf("Enter number of rows and columns of matrix 2\n");
scanf("%d %d",&rows_2,&col_2);
int a1[rows_1][col_1],a2[rows_2][col_2],mul[rows_1][col_1];
if(col_1==rows_2)
{
printf("Enter matrix 1\n");
for(i=0;i<rows_1;i++)
{
for(j=0;j<col_1;j++)
{
canf("%d",&a1[i][j]);
}
}
printf("Enter matrix 2\n");
for(i=0;i<rows_1;i++)
{
for(j=0;j<col_1;j++)
{
scanf("%d",&a2[i][j]);
}
}
for(i=0;i<rows_1;i++)
{
for(j=0;j<col_1;j++)
{
for(k=0;k<rows_2;k++)
{
sum+=a1[i][k]*a2[k][j];
}
mul[i][j]=sum;
sum=0;
}
}
printf("Multiplication of above matrices is\n");
for(i=0;i<rows_1;i++)
{
for(j=0;j<col_2;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}
else
{
printf("not possible with the given rows and column");
}
getch();
}
SAMPLE OUTPUT :
RESULT :
Thus , the C program to perform matrix multiplication has been executed successfully .
EX. NO - 2(D)
AIM :
ALGORITHM :
STEP 1: Start.
STEP 7: Stop.
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[25][25],row,col;
printf("Enter the number of rows and column of matrix: ");
scanf("%d%d",&row,&col);
printf("Enter elements in matrix:
"); for(i=0;i<row;i++)
for(j=0;j<col;j++) scanf("%d",&a[i]
[j]);
printf("The given matrix\
n"); for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
printf("\t%d",a[i][j]);
}
printf("\n\nTranspose of the matrix\n");
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++) printf("\t
%d",a[j][i]);
}
getch();
}
SAMPLE OUTPUT :
RESULT :
Thus , the C program to perform transpose of the matrix has been executed
successfully .