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

0 - CSB Lab

Uploaded by

Manish Kumar
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)
28 views

0 - CSB Lab

Uploaded by

Manish Kumar
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/ 18

Question - Write a program to read a string (word), store it in an array and

obtain its reverse by using a user defined function.

#include <stdio.h>
#include <string.h>

int main()
{
char s[1000];
int temp;

printf("Enter the string: ");


gets(s);
int len = strlen(s);

printf("Entered string is %s\n", s);

for (int i = 0; i < len / 2; i++)


{
temp = s[i];
s[i] = s[len - i - 1];
s[len - i - 1] = temp;
}

printf("Reversed string is %s\n", s);

return 0;
}
Question - Write a menu driven program to read a matrix.

#include <stdio.h>

void matrix(int r, int c)


{
int arr1[r][c], arr2[r][c];

printf("Matrix \n");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("Enter the %d%d element: ", i, j);
scanf("%d", &arr1[i][j]);
}
}

int main()
{
int r, c;
printf("Enter the number of rows: ");
scanf("%d", &r);

printf("Enter the number of columns: ");


scanf("%d", &c);
printf("Enter the matrix: \n");
matrix(r, c);

return 0;
}

Question - Write a program to find the sum of two matrices.

#include <stdio.h>

void matrix(int r, int c)


{
int arr1[r][c], arr2[r][c], arr3[r][c];

printf("Matrix 1\n");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("Enter the %d%d element: ", i, j);
scanf("%d", &arr1[i][j]);
}
}

printf("Matrix 2\n");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("Enter the %d%d element: ", i, j);
scanf("%d", &arr2[i][j]);
}
}

for (int i = 0; i < r; i++)


{
for (int j = 0; j < c; j++)
{
arr3[i][j] = arr1[i][j] + arr2[i][j];
}
}

printf("The matrix after addition is\n");


for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("%d ", arr3[i][j]);
}
printf("\n");
}
}

int main()
{
int r, c;
printf("Enter the number of rows: ");
scanf("%d", &r);

printf("Enter the number of columns: ");


scanf("%d", &c);

printf("Enter the matrix: \n");


matrix(r, c);

return 0;
}
Question - Write a program to find products of two matrices.
#include <stdio.h>

int main()
{
int a, b, c, d;

printf("Matrix1\n");
printf("Enter the number of rows: ");
scanf("%d", &a);

printf("Enter the number of columns: ");


scanf("%d", &b);

printf("Matrix2\n");
printf("Enter the number of rows: ");
scanf("%d", &c);

printf("Enter the number of columns: ");


scanf("%d", &d);

int matrix3[a][d];
if (b != c)
{
printf("The given matrices cannot be multiplied\n");
}

else
{
int matrix1[a][b];
printf("Matrix 1\n");
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
printf("Enter the %d%d element: ", i, j);
scanf("%d", &matrix1[i][j]);
}
}

printf("Matrix 2\n");
int matrix2[c][d];
for (int i = 0; i < c; i++)
{
for (int j = 0; j < d; j++)
{
printf("Enter the %d%d element: ", i, j);
scanf("%d", &matrix2[i][j]);
}
}
for (int i = 0; i < a; i++)
{
for (int j = 0; j < d; j++)
{
matrix3[i][j] = 0;
for (int k = 0; k < c; k++)
{
matrix3[i][j] = matrix3[i][j] + matrix1[i][k] * matrix2[k][j];
}
}
}

for (int p = 0; p < a; p++)


{
for (int q = 0; q < d; q++)
{
printf("%d\t", matrix3[p][q]);
}
printf("\n");
}
}
return 0;
}

Question. Write a program to find the transpose of a matrix.


#include <stdio.h>
void matrixtranspose(int r, int c)
{
int arr1[r][c];
printf("Enter the matrix: \n");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("Enter the %d%d element: ", i, j);
scanf("%d", &arr1[i][j]);
}
}

printf("The entered matrix is \n");


for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("%d ", arr1[i][j]);
}
printf("\n");
}

int arr2[c][r];

for (int i = 0; i < r; i++)


{
for (int j = 0; j < c; j++)
{
arr2[i][j] = arr1[j][i];
}
}
printf("The transposed matrix is \n");

for (int i = 0; i < r; i++)


{
for (int j = 0; j < c; j++)
{
printf("%d ", arr2[i][j]);
}
printf("\n");
}
}

int main()
{
int r, c;
printf("Enter the number of rows: ");
scanf("%d", &r);

printf("Enter the number of columns: ");


scanf("%d", &c);

matrixtranspose(r, c);

return 0;
}
Question - Write a program to add two numbers using pointers.

#include <stdio.h>
int main()
{
int a, b;
int *ptr1 = &a;
int *ptr2 = &b;

printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);

int sum = (*ptr1) + (*ptr2);


printf("Sum of both numbers is : %d\n", sum);
return 0;
}
Question - Write a program to swap two numbers using user defined functions
and pointers.

#include <stdio.h>
int main()
{
int a, b;
int *ptr1 = &a;
int *ptr2 = &b;

printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);

int temp = *ptr1;


*ptr1 = *ptr2;
*ptr2 = temp;

printf("a=%d b=%d \n", a, b);


return 0;
}
Question - Compute sum of the elements stored in an array using pointers
and user defined function.

#include <stdio.h>
int main()
{
int n, sum = 0;
printf("Enter n: ");
scanf("%d", &n);
int arr[n];

for (int i = 0; i < n; i++)


{
printf("Enter the element %d: ", i+1);
scanf("%d", &arr[i]);
}

for (int j = 0; j < n; j++)


{
int *ptr = &arr[j];
sum = sum + (*ptr);
}
printf("Sum is : %d\n",sum);

return 0;
}
Question - Write a program to create a structure named company which has name,
address, phone and noOfEmployee as member variables. Read the name of the
company, its address and phone and noOfEmployee. Finally display these members'
values.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct company
{
char name[50];
char address[500];
int phone;
int employeeno;
};

int main()
{
struct company c1;

printf("Enter the company name: ");


scanf("%[^\n]%*c", c1.name);
printf("Enter the address of company: ");
scanf("%[^\n]%*c", c1.address);
printf("Enter the phone number of the company: ");
scanf("%d", &c1.phone);
printf("Enter the number of employees: ");
scanf("%d", &c1.employeeno);

printf("%s\n", c1.name);
printf("%s\n", c1.address);
printf("%d\n", c1.phone);
printf("%d\n", c1.employeeno);

return 0;
}
Question - Define a structure "complex" (typedef) to read two complex
numbers and perform addition, subtraction of these two complex numbers
and display the result.

#include <stdio.h>

typedef struct complexfunctions


{
float real;
float img;
} complex;

int main()
{

complex num1, num2;

printf("Enter the real part of first number: ");


scanf("%f", &num1.real);

printf("Enter the imaginary part of first number: ");


scanf("%f", &num1.img);

printf("Enter the real part of second number: ");


scanf("%f", &num2.real);

printf("Enter the imaginary part of second number: ");


scanf("%f", &num2.img);

printf("The addition is: %f+i(%f)\n", num1.real + num2.real, num1.img + num2.img);


printf("The subtraction is: %f+i(%f)\n", num1.real - num2.real, num1.img - num2.img);

return 0;
}
Question - Write a program to add two distances in feet and inches using
structure.

#include <stdio.h>

struct add_distances
{
float feet;
float inches;
};

int main()
{

struct add_distances d1, d2;

printf("Enter the feet part of first distance: ");


scanf("%f", &d1.feet);

printf("Enter the inches part of first distance: ");


scanf("%f", &d1.inches);
printf("Enter the feet part of second distance: ");
scanf("%f", &d2.feet);

printf("Enter the inches part of second distance: ");


scanf("%f", &d2.inches);

int a, b, c;
a = d1.feet + d2.feet;
b = d1.inches + d2.inches;
if (b >= 12)
{
c = b / 12;
a = a + c;
b = b - c * 12;
}
printf("%d Feet and %d Inches\n", a, b);
return 0;
}

Question - Write a program to create a file called emp.ree and store information
about a person, in terms of his name, age and salary.

#include <stdio.h>

int main()
{
FILE *fptr;
char name[25];
int age;
float salary;

fptr = fopen("emp.rec", "w");

if (fptr == NULL)
{
printf("File does not exist\n");
}
printf("Enter the name\n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);

printf("Enter the age\n");


scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);

printf("Enter the salary\n");


scanf("%f", &salary);
fprintf(fptr, "Salary = %f\n", salary);

fclose(fptr);
return 0;
}

You might also like