0% found this document useful (0 votes)
12 views8 pages

Cse115 Lab Manual 3 Operators Part2

The document contains a lab manual for CSE 115, featuring practice problems and exercises focused on C programming, particularly using bitwise operators, arithmetic operations, and geometric calculations. It includes example C programs demonstrating various concepts such as bitwise operations, surface area calculations, and user input handling. Additionally, it provides exercise and homework questions for students to reinforce their understanding of the material.

Uploaded by

safiulbasar51
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)
12 views8 pages

Cse115 Lab Manual 3 Operators Part2

The document contains a lab manual for CSE 115, featuring practice problems and exercises focused on C programming, particularly using bitwise operators, arithmetic operations, and geometric calculations. It includes example C programs demonstrating various concepts such as bitwise operations, surface area calculations, and user input handling. Additionally, it provides exercise and homework questions for students to reinforce their understanding of the material.

Uploaded by

safiulbasar51
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/ 8

Lab Manual 3 CSE 115 (ARA2)

Practice Problems:
C programs to demonstrate bitwise operators:
1. #include <stdio.h>
void main()
{
int i = 3, num=48;
printf("Right shift by %d: %d\n",i,num>>i);
printf("\n");
printf("Left shift by %d: %d\n",i,num<<i);
}
2. (same as above, except now we are using hexadecimal values)
#include <stdio.h>
void main()
{
int i = 0x3, num=0x30;
printf("Right shift by %x: %x\n",i,num>>i);
printf("\n");
printf("Left shift by %x: %x\n",i,num<<i);
}
3. #include <stdio.h>
void main()
{
int a=12,b=39;
printf("AND=%d",a&b);
printf("\nOR=%d",a|b);
printf("\nXOR=%d",a^b);
}

4. #include<stdio.h>
void main()
{
char c = 105;
printf("%X", ~c);//1’s complement of c
printf("\n%X", -c); //2’s complement of c
}

5. C Program to demonstrate the usage of assignment and increment operators:


#include<stdio.h>
void main()
{
char a;
printf("enter a lowercase letter:");
scanf("%c",&a);
a-=32;//not recommended, since it replaces the original character
printf("Uppercase of given letter is: %c",a);
++a;//not recommended, …
printf("\nUppercase of next letter is: %c",a);
}
6. C Program to find surface area of a sphere:
#include <stdio.h>

#define PI 3.14

int main()
{
float radius, sa;
printf("\n Please Enter the radius of a Sphere \n");
scanf("%f", &radius);

sa = 4 * PI * radius * radius;

printf("\n The Surface area of a Sphere = %.2f", sa);


}

Exercise Problems:
1. Read an integer number n from user. Compute the bitwise AND of n and 1. Do you see a
pattern in the result? (Hint: observe the difference in the result when n is odd vs. when n
is even)
2. Read two integer numbers m, n from user. Compute the value of m*2 n as well as the
value of m <<n. Do you see a pattern in the result? Now compute the value of m/2 n as
well as the value of m >>n. Do you see a pattern?
3. Compute the volume of a sphere; read the radius from user.
4. Read the co-ordinates of two points (x1,y1) and (x2,y2) from user. Compute the
midpoints of these two points and print it up to 2 decimal points.
5. Find the angle of a segment in a circle; read the arc length and radius from user.
Homework Questions:
1. Compute the area of a (a) trapezoid and (b) parallelogram. Read necessary inputs from
user.
2. Compute the volume and surface area of a cone. Read the radius and height of the cone
from user.
3. Read the lengths of base and height of a right angle triangle. Then compute the length of
its hypotenuse using Pythagorean theorem.
4. Write a C program to count total number of notes in given amount.
Tentative Input/Output (bold ones are user inputs):
Enter amount: 1176
Total number of notes:
500: 2
100: 1
50: 1
20: 1
10: 0
5: 1
2: 0
1: 1

Exercise Problems

1. Read an integer number n from the user. Compute the bitwise AND of n and 1. Do you see
a pattern in the result?

c
Copy
#include <stdio.h>

int main() {
int n;

// Ask the user to input an integer


printf("Enter an integer n: ");
scanf("%d", &n);

// Compute the bitwise AND of n and 1


int result = n & 1;

// Print the result


printf("The result of n & 1 is: %d\n", result);

// Observe the pattern


if (n % 2 == 0) {
printf("n is even, so n & 1 = 0.\n");
} else {
printf("n is odd, so n & 1 = 1.\n");
}

return 0;
}

 Explanation: The result of n & 1 is 1 if n is odd and 0 if n is even.

2. Read two integer numbers m, n from the user. Compute m * 2^n and m << n. Also compute
m / 2^n and m >> n.

c
Copy
#include <stdio.h>

int main() {
int m, n;

// Ask the user to input m and n


printf("Enter values for m and n: ");
scanf("%d %d", &m, &n);

// Compute m * 2^n and m << n


int multiply_result = m * (1 << n);
int shift_left_result = m << n;

// Compute m / 2^n and m >> n


int divide_result = m / (1 << n);
int shift_right_result = m >> n;
// Print the results
printf("m * 2^n = %d (Using multiplication)\n", multiply_result);
printf("m << n = %d (Using left shift)\n", shift_left_result);
printf("m / 2^n = %d (Using division)\n", divide_result);
printf("m >> n = %d (Using right shift)\n", shift_right_result);

return 0;
}

 Explanation: The shift operations (<< and >>) are equivalent to multiplying and dividing by
powers of 2. You can observe the patterns between the two methods.

3. Compute the volume of a sphere; read the radius from the user.

c
Copy
#include <stdio.h>
#include <math.h>

int main() {
double radius, volume;

// Ask the user to enter the radius


printf("Enter the radius of the sphere: ");
scanf("%lf", &radius);

// Compute the volume of the sphere


volume = (4.0 / 3.0) * M_PI * pow(radius, 3);

// Print the volume


printf("The volume of the sphere is: %.2lf\n", volume);

return 0;
}

 Explanation: The formula for the volume of a sphere is V = (4/3) * π * r^3.

4. Read the coordinates of two points (x1, y1) and (x2, y2) from the user. Compute the
midpoint.

c
Copy
#include <stdio.h>

int main() {
double x1, y1, x2, y2, midpoint_x, midpoint_y;

// Ask the user to input coordinates of the two points


printf("Enter the coordinates of the first point (x1, y1): ");
scanf("%lf %lf", &x1, &y1);
printf("Enter the coordinates of the second point (x2, y2): ");
scanf("%lf %lf", &x2, &y2);

// Compute the midpoint


midpoint_x = (x1 + x2) / 2;
midpoint_y = (y1 + y2) / 2;
// Print the midpoint
printf("The midpoint of the two points is: (%.2lf, %.2lf)\n", midpoint_x,
midpoint_y);

return 0;
}

 Explanation: The midpoint formula is ((x1 + x2) / 2, (y1 + y2) / 2).

5. Find the angle of a segment in a circle; read the arc length and radius from the user.

c
Copy
#include <stdio.h>
#include <math.h>

int main() {
double arc_length, radius, angle;

// Ask the user to input arc length and radius


printf("Enter the arc length: ");
scanf("%lf", &arc_length);
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);

// Compute the angle in radians


angle = arc_length / radius;

// Convert angle to degrees


double angle_degrees = angle * (180.0 / M_PI);

// Print the angle in radians and degrees


printf("The angle of the segment is: %.2lf radians (%.2lf degrees)\n",
angle, angle_degrees);

return 0;
}

 Explanation: The formula for the angle in radians is θ = arc_length / radius. The
angle can also be converted to degrees using the formula degrees = radians * (180 /
π).

Homework Questions

1. Compute the area of a (a) trapezoid and (b) parallelogram.

c
Copy
#include <stdio.h>

int main() {
double base1, base2, height, area;

// For trapezoid
printf("Enter base1, base2, and height of trapezoid: ");
scanf("%lf %lf %lf", &base1, &base2, &height);

area = ((base1 + base2) * height) / 2;


printf("The area of the trapezoid is: %.2lf\n", area);

// For parallelogram
printf("Enter base and height of parallelogram: ");
scanf("%lf %lf", &base1, &height);

area = base1 * height;


printf("The area of the parallelogram is: %.2lf\n", area);

return 0;
}

 Explanation: The area of a trapezoid is ((base1 + base2) * height) / 2 and the area
of a parallelogram is base * height.

2. Compute the volume and surface area of a cone.

c
Copy
#include <stdio.h>
#include <math.h>

int main() {
double radius, height, volume, surface_area;

// Ask the user for radius and height


printf("Enter the radius and height of the cone: ");
scanf("%lf %lf", &radius, &height);

// Compute the volume of the cone


volume = (M_PI * pow(radius, 2) * height) / 3;

// Compute the surface area of the cone


surface_area = M_PI * radius * (radius + sqrt(pow(radius, 2) + pow(height,
2)));

// Print the results


printf("The volume of the cone is: %.2lf\n", volume);
printf("The surface area of the cone is: %.2lf\n", surface_area);

return 0;
}

 Explanation: Volume: V = (π * r^2 * h) / 3. Surface area: A = π * r * (r + √(r^2


+ h^2)).

3. Compute the length of the hypotenuse of a right-angle triangle using Pythagorean theorem.

c
Copy
#include <stdio.h>
#include <math.h>
int main() {
double base, height, hypotenuse;

// Ask the user for the base and height


printf("Enter the base and height of the triangle: ");
scanf("%lf %lf", &base, &height);

// Compute the length of the hypotenuse


hypotenuse = sqrt(pow(base, 2) + pow(height, 2));

// Print the result


printf("The length of the hypotenuse is: %.2lf\n", hypotenuse);

return 0;
}

 Explanation: The Pythagorean theorem states that hypotenuse^2 = base^2 + height^2.

4. Count the total number of notes in a given amount.

c
Copy
#include <stdio.h>

int main() {
int amount;

// Ask the user for the amount


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

// Calculate the number of notes for each denomination


int notes500 = amount / 500;
amount %= 500;

int notes100 = amount / 100;


amount %= 100;

int notes50 = amount / 50;


amount %= 50;

int notes20 = amount / 20;


amount %= 20;

int notes10 = amount / 10;


amount %= 10;

int notes5 = amount / 5;


amount %= 5;

int notes2 = amount / 2;


amount %= 2;

int notes1 = amount;

// Print the results


printf("Total number of notes:\n");
printf("500: %d\n", notes500);
printf("100: %d\n", notes100);
printf("50: %d\n", notes50);
printf("20: %d\n", notes20);
printf("10: %d\n", notes10);
printf("5: %d\n", notes5);
printf("2: %d\n", notes2);
printf("1: %d\n", notes1);

return 0;
}

 Explanation: The program divides the given amount by the largest possible note (500, 100,
etc.) and then calculates how many of each note are needed.

You might also like