0% found this document useful (0 votes)
30 views7 pages

Lab 8 SNB

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

Lab 8 SNB

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

PROBLEM SOLVING AND COMPUTER PROGRAMMING

LAB 8

NAME: SANJEEV NAIK BANOTHU

ROLL NO: 221220052

1. Declare a union containing 5 string variables (Name, House Name, City Name, State
and Pin
code) each with a length of C_SIZE (user defined constant). Then, read and display the
address of a person using a variable of the union.

SOURCE CODE

#include<stdio.h>

#define c_size 100


union address{
char name[c_size];
char housename[c_size];
char city[c_size];
char state[c_size];
char pincode[c_size];
};
int main(){
union address user;
printf("ENTER THE ADDRESS :");
scanf("%s",&user.name);
printf("NAME :%s\n",user.name);
scanf("%s",&user.housename);
printf("HOUSE NAME :%s\n",user.housename);
scanf("%s",&user.city);
printf("CITY :%s\n",user.city);
scanf("%s",&user.state);
printf("STATE :%s\n",user.state);
scanf("%s",&user.pincode);
printf("PINCODE :%s\n",user.pincode);
return 0;
}

OUTPUT
2. Find the factorial of a given Natural Number n using recursive and non-recursive
function.

SOURCE CODE

#include <stdio.h>
#include<math.h>
long int fact(int n);
long int factorial(int n);
int main(){
float x;
printf("enter the number:");
scanf("%f",&x);
printf("%ld\n" , fact(x));
printf("%ld\n" , factorial(x));
return 0;
}
long int factorial(int n){
printf("USING NON REECURSIVE FUNCTION\n");
long int fact=1;
for(int i=1;i<=n;i++){
fact*=i;
}
return fact;
}
long int fact(int n){
if(n==0){
printf("USING RECURSIVE FUNCTION\n");
return 1;
}
else return n*fact(n-1);
}

OUTPUT
3. Read a string (word), store it in an array and obtain its reverse by using a user defined
function.

SOURCE CODE

#include<stdio.h>

#include<string.h>

void string_reversal(char a[1000]);


int main(){
char a[1000],ch;
printf("enter the string:");
scanf("%s",a);
printf("original string %s\n",a);
string_reversal(a);
return 0;
}
void string_reversal(char a[1000]){
char ch;
int n=strlen(a);
for(int i=0;i<n/2;i++){
ch=a[i];
a[i]=a[n-1-i];
a[n-1-i]=ch;
}
printf("string after reversal %s\n",a);
}

OUTPUT
4.Write a menu driven program for performing matrix addition, multiplication and finding the
transpose. Use functions to (I) read a matrix, (II) find the sum of two matrices, (III) find the
product of two matrices, (IV) find the transpose of a matrix and (V) display a matrix.

SOURCE CODE

#include<stdio.h>

void matrix_multiplication(int m,int n,int a[m][n],int b[m][n]);


void matrix_transpose(int m,int n,int a[m][n]);
void matrix_add(int m,int n,int a[m][n],int b[m][n]);
void matrix(int n,int m);
int main(){
int a,b;
printf("enter rows and colums of two matrices:");
scanf("%d %d",&a,&b);
matrix(a,b);
return 0;
}
void matrix_multiplication(int m,int n,int a[m][n],int b[m][n]){
int result[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
result[i][j]=0;
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
result[i][j]+=a[i][k]*b[k][j];
}
}
}
printf("resultant matrix after multiplication of A and B\n");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
printf("%d ",result[i][j]);
}
printf("\n");
}
}
void matrix_transpose(int m,int n,int a[m][n]){
int transpose[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
transpose[i][j]=a[j][i];
}
}
printf("resultant matrix after transpose of A \n");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%d ",transpose[i][j]);
}
printf("\n");
}
}
void matrix_add(int m,int n,int a[m][n],int b[m][n]){
int add[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
add[i][j]=a[i][j]+b[i][j];
}
}
printf("resultant matrix after addition of A and B\n");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
printf("%d ",add[i][j]);
}
printf("\n");
}
}
void matrix(int n,int m){
int a[m][n],b[m][n];
printf("enter elements of A :");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
printf("enter elements of B :");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
scanf("%d",&b[i][j]);
}
}
printf("1.display of matrices\n");
printf("2.addition of two matrices\n");
printf("3.multiplication of two matrices\n");
printf("4.transpose of matrix\n");
while(1){
int choice;
printf("enter your choice :");
scanf("%d",&choice);
if(choice==2){
matrix_add(m,n,a,b);
}
else if(choice==3){
matrix_multiplication(m,n,a,b);
}
else if(choice==4){
matrix_transpose(m,n,a);
}
else if(choice==1){
printf("first matrix A\n");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("second matrix is B\n");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
printf("%d ",b[i][j]);
}
printf("\n");
}
}
else {
exit(0);
}
}

}
OUTPUT

You might also like