Cse115 Lab Manual 3 Operators Part2
Cse115 Lab Manual 3 Operators Part2
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
}
#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;
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;
return 0;
}
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;
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;
return 0;
}
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;
return 0;
}
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;
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
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);
// For parallelogram
printf("Enter base and height of parallelogram: ");
scanf("%lf %lf", &base1, &height);
return 0;
}
Explanation: The area of a trapezoid is ((base1 + base2) * height) / 2 and the area
of a parallelogram is base * height.
c
Copy
#include <stdio.h>
#include <math.h>
int main() {
double radius, height, volume, surface_area;
return 0;
}
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;
return 0;
}
c
Copy
#include <stdio.h>
int main() {
int amount;
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.