You are on page 1of 3

LAB MANUAL # 3

Course: Computer Programming (CP-107)


Session: 20CP

Branching statements (if-else, nested if-else,


multiway if-else)

This lab has been designed to reinforce concepts of branching mechanism for changing
program flow of control. Clear concept about this programming construct has already been
provided in the class. Supporting material can be found in section 2.4, 3.1 and 3.2 of Problem
solving with C++. Lab handouts are also provided along with the lab manual as reference
material.
Objectives:
In this lab, you will practice:
• Creating and executing Boolean expression by using relational and logical operators.
• Using if-else statement for problems that require making choice between two
alternatives.
• Using if-else with single as well as compound statements.
• Using nested if-else statement.
• Using multiway if-else statement for making choice among more than two
alternatives.

Lab Tasks:
Question # 1:
Write a program to check whether an integer entered by the user is positive or negative.
Question # 2:
Write a program to check whether an integer entered by the user is prime number or not.
Question # 3
Write a program to find if an integer entered by the user is even or odd or neither (0), using
nested if-else statement.
Question # 4:
Write a program to prompt the user to enter an alphabet and then tell whether entered
character is vowel or consonant.
Question # 5:
The Harris–Benedict equation estimates the number of calories your body needs to maintain
your weight if you do not exercise. This is called your basal metabolic rate, or BMR.
LAB MANUAL # 3
Course: Computer Programming (CP-107)
Session: 20CP

The formula for the calories needed for a woman to maintain her weight is:
BMR = 655 + (4.3 × weight in pounds) + (4.7 × height in inches) – (4.7 × age in years)
The formula for the calories needed for a man to maintain his weight is:
BMR = 66 + (6.3 × weight in pounds) + (12.9 × height in inches) – (6.8 × age in years)
A typical chocolate bar will contain around 230 calories. Write a program that allows the user
to input his or her weight in pounds, height in inches, age in years, and the character M for
male and F for female. The program should then output the number of chocolate bars that
should be consumed to maintain one’s weight for the appropriate sex of the specified weight,
height, and age.
Question # 6:
Write a C++ program that solves a quadratic equation to find its roots. The roots of a quadratic
equation
ax2 + bx + c = 0
are given by the formula

−𝒃 ± √𝒃𝟐 − 𝟒𝒂𝒄
−𝒃 ±
𝟐𝒂
The value of the discriminant 𝑏 2 − 4𝑎𝑐 determines the nature of roots. If the value of the
discriminant is zero, then the equation has a single real root. If the value of the discriminant
is positive, then the equation has two real roots. If the value of the discriminant is negative,
then the equation has two complex roots. The program takes values of a, b, and c as input
and outputs the roots. Be creative in how you output complex roots.
Question # 7:
Write an astrology program. The user types in a birthday, and the program responds with the
sign and horoscope for that birthday. The month may be entered as a number from 1 to 12.
Then enhance your program so that if the birthday is only one or two days away from an
adjacent sign, the program announces that the birthday is on a “cusp” and also outputs the
horoscope for that nearest adjacent sign.
The horoscope signs and dates are:
Aries March 21–April 19
Taurus April 20–May 20
Gemini May 21–June 21
Cancer June 22–July 22
LAB MANUAL # 3
Course: Computer Programming (CP-107)
Session: 20CP

Leo July 23–August 22


Virgo August 23–September 22
Libra September 23–October 22
Scorpio October 23–November 21
Sagittarius November 22–December 21
Capricorn December 22–January 19
Aquarius January 20–February 18
Pisces February 19–March 20
Question # 8:
Write a program that determines whether character entered by the user is a lower-case letter,
an upper-case letter or a digit.

***************

You might also like