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

EXP2

C programming and Data Structures lab

Uploaded by

Anuja S
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)
21 views

EXP2

C programming and Data Structures lab

Uploaded by

Anuja S
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/ 12

EX.

NO - 2(A)
DATE : FACTORIAL USING RECURSION

AIM :
To write a C program on factorial of a number using recursion .

ALGORITHM :

STEP 1: Start the program.

STEP 2: Get n value.

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).

STEP 5: Print the value of factorial.

STEP 6: Stop the program.


PROGRAM :

#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 2: Get i, b, c, d, a and j values.

STEP 3: Enter the number of rows & columns required for a matrix.

STEP 4: Enter the values.

STEP 5: Compute sum (Addition) Operator.

STEP 6: Print the 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 :

Please enter the number of rows of matrix


2
Please enter number of columns of matrix
2
Please enter the elements of first matrix one by one
3
3
3
3
Please enter the elements of second matrix one by
one 4
4
4
4
The sum of entered matrices is below:
7 7
7 7

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 2: Get i, b, c, d, a and j values.

STEP 3: Enter the number of rows & columns required for a matrix.

STEP 4: Enter the values.

STEP 5: Compute Multiplication Operation.

STEP 6: Print the 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 :

Enter number of rows and columns of matrix 1


2
2
Enter number of rows and columns of matrix 2
2
2
Enter matrix 1
2
2
2
2
Enter matrix 2
2
2
2
2
Multiplication of above matrices is
8 8
8 8

RESULT :

Thus , the C program to perform matrix multiplication has been executed successfully .
EX. NO - 2(D)

DATE : TRANSPOSE OF MATRIX

AIM :

To write a C program on transpose of matrix operation .

ALGORITHM :

STEP 1: Start.

STEP 2: Get i, j, a, row & col values.

STEP 3: Enter the number of rows & columns required.

STEP 4: Enter the matrix values.

STEP 5: Perform Transpose operation.

STEP 6: Print the matrix.

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 :

Enter the number of rows and column of matrix:


3
3
Enter elements in matrix:
1
2
3
4
5
6
7
8
9

The given matrix


1 2 3
4 5 6
7 8 9
Transpose of matrix
1 4 7
2 5 8
3 6 9

RESULT :

Thus , the C program to perform transpose of the matrix has been executed
successfully .

You might also like