Lab Sheet 03
Lab Sheet 03
2. Write a function which checks whether page of a book is in left side or right side when page
number is given. Note that numbering starts from the very first page.
1
3. Write a program to check if a number is even or odd.
4. Write a program called PrintNumberInWord which prints "ONE", "TWO", ... , "NINE", "OTHER" if
the int variable "number" is 1, 2,... , 9, or other, respectively. Use a "nested-if" statement.
5. Given the float variables x1, x2, y1, y2 write a code segment to find the slope of a line through
the two points (x1, y1) and (x2, y2). Note that you have to check whether slope is undefined.
6. Write a C program to calculate the revenue from a sale based on the unit price and quantity of a
product input by the user.
The discount rate is 15% for the quantity purchased between 120 and 160 units, and 20% for the
quantity purchased greater than 160 units. If the quantity purchased is less than 120 units, the
discount rate is 0%.
7. Print "Uppercase", "Lowercase", or "Not a letter" depending on whether the character input by
the user is an uppercase alphabetic character, a lowercase alphabetic character, or not an
alphabetic character at all.
11. Body constitution is measured using BMI (Body Mass Index) which depends only on the height
and weight of a person. It can be defined by,
Where weight is taken in kilograms and height in meters. Four general grades are proposed such
that,
Underweight : BMI < 18.5
Normal weight : 18.5 <= BMI < 25.0
2
Overweight : 25.0 <= BMI < 30.0
Obesity : 30.0 <= BMI
Write a C program to output user’s body constitution grade when he/she inputs body weight (kg)
and the height (m).
User will have a number between 0 and 100 in his or her head. Write a C program to guess that
number. Program will guess a number and users will say whether it is too high, too low, or that’s
the number. At the end, print how many guesses it took to get right number. Note: You will have
to choose how your program will strategically guess. A naïve strategy can be to simply start the
guessing at 1, and keep going (2, 3, 4, etc.) until you hit the number. But that’s not an optimal
guessing strategy. You need to provide an optimized solution.
3
15. Following pseudocode shows how to find all the roots of a quadratic equation ax2+bx+c=0.
Convert it to a C program.
Step 1: Start
Step 2: Read A, B, C as integer
Step 3: Declare disc, deno, x1, x2 as float
Step 4: Assign disc = (B * B) - (4 * A * C)
Step 5: Assign deno = 2 * A;
Step 6: if( disc > 0 )
begin
Print “THE ROOTS ARE REAL ROOTS”
Assign x1 ← (-B / deno) + (sqrt(disc) / deno)
Assign x2 ← (-B / deno) - (sqrt(disc) / deno)
Print x1, x2
end
else if(disc = 0)
begin
Print “ THE ROOTS ARE REPEATED ROOTS"
Assign x1 ← -B / deno
Print x1
end
else Print “THE ROOTS ARE IMAGINARY ROOTS”
Step7: Stop
Hint: Nature of roots of quadratic equation can be known from the quadrant = b2-4ac
If b2-4ac >0 then roots are real and unequal
If b2-4ac =0 then roots are real and equal
If b2-4ac <0 then roots are imaginary