0% found this document useful (0 votes)
41 views3 pages

Part 3 - Selection

This document provides instructions and sample code for exercises using selection structures in C++. Students are to complete 13 exercises that involve writing programs to: 1) output text based on user's language choice, 2) output a name based on a character input, and 3) perform other tasks like determining quadrant of an angle, number of days in a month, grade based on score, marriage status from code, temperature conversions, and properties of triangles and numbers. The exercises increase in complexity and cover using if/else statements and switch statements.
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)
41 views3 pages

Part 3 - Selection

This document provides instructions and sample code for exercises using selection structures in C++. Students are to complete 13 exercises that involve writing programs to: 1) output text based on user's language choice, 2) output a name based on a character input, and 3) perform other tasks like determining quadrant of an angle, number of days in a month, grade based on score, marriage status from code, temperature conversions, and properties of triangles and numbers. The exercises increase in complexity and cover using if/else statements and switch statements.
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/ 3

HCMC UNIVERSITY OF TECHNOLOGY, VNU-HCM

Faculty of Computer Science and Engineering

Lab 4

In this week, we practice implementing in C++ small code with selection structure. Students can see
some sample exercises and must prepare solution for all exercises in part B.

A. Sample exercises.

Exercise 1. Write a program to say hello on the screen in the user language.

SOLUTION:
#include <iostream>
using namespace std;

int main(){
char lang;
cin >> "Where do you come from ";
cin >> "(V = Vietnam; A = American; F = France): ";
cin >> lang;

if (lang==’A’)
cout << "Hello ! ";
else if (lang==’F’)
cout << "Bonjour ! ";
else
cout << "Xin chao ! ";

cout << endl;

return 0;
}

Exercise 2. Write and run a program that reads a character and writes out a name corresponding to
the character:
if the character is ‘A’ or ‘a’ then the name is “An”
if the character is ‘B’ or ‘b’ then the name is “Bao”
if the character is ‘C’ or ‘c’ then the name is “Chi”
if the character is ‘D’ or ‘d’ then the name is “Dong”
otherwise, the name is just the character.

SOLUTION:

Introduction to Computing CO1005 – Semester 1 of 2019-2020 1/3


HCMC UNIVERSITY OF TECHNOLOGY, VNU-HCM
Faculty of Computer Science and Engineering

#include <iostream>
using namespace std;

int main() {
string ch;
cout<< "Type your character you want: "; cin>>ch;

// Processing with ch name


if (ch == "A" || ch == "a") {
cout<< "The name is An" <<endl;
}
else if (ch == "B" || ch == "b") {
cout<< "The name is Bao" <<endl;
}
else if (ch == "C" || ch == "c") {
cout<< "The name is Chi" <<endl;
}
else if (ch == "D" || ch == "D") {
cout<< "The name is Dong" <<endl;
}
else {
cout<< "The name is just " <<ch<<endl;
}

system("pause");
return 0;
}

B. Exercises must to do.

Exercise 3. Write and run a program that reads an angle (expressed in degrees) and states in which
quadrant the given angle lies. An angle A is said to be in the
first quadrant if it is in the range 0 ≤ A < 90
second quadrant if it is in the range 90 ≤ A < 180
third quadrant if it is in the range 180 ≤ A < 270
and fourth quadrant if it is in the range 270 ≤ A < 360.

Exercise 4. Write and run a program that reads a month from the user and displays the number of
days in that month.

Exercise 5. Write and run a program that reads a numeric grade from a student and displays a

Introduction to Computing CO1005 – Semester 1 of 2019-2020 2/3


HCMC UNIVERSITY OF TECHNOLOGY, VNU-HCM
Faculty of Computer Science and Engineering

corresponding character grade for that numeric grade. The program prints ‘A’ for exam
grades greater than or equal to 90, ‘B’ for grades in the range 80 to 89, ‘C’ for grades in the
range 70 to 79, ‘D’ for grades in the range 60 to 69 and ‘F’ for all other grades.

Exercise 6. Write and run a program that reads a marriage code (one character) and writes out a
message corresponding to the character:
if the character is ‘M’ or ‘m’ then the message is “Individual is married”
if the character is ‘D’ or ‘d’ then the message is “Individual is divorced”
if the character is ‘W’ or ‘w’ then the message is “Individual is widowed”
otherwise, the message is “An invalid code was entered”.
(Hint: use switch statement).

Exercise 7. Write and run a program that gives the user only three choices: convert from
Fahrenheit to Celsius, convert from Celsius to Fahrenheit, or quit. If the third choice is
selected, the program stops. If one of the first two choices is selected, the program should
prompt the user for either a Fahrenheit or Celsius temperature, as appropriate, and then
compute and display a corresponding temperature. Use the conversion formulas:
F = (9 / 5) . C + 32
C = (5 / 9) . (F - 32)

Exercise 8. Write a program to calculate the number of results obtained when solving the
quadratic equation: ax2 + bx + c =0 with given real inputs a, b, and c.
Exercise 9. Write a program to find the largest number among three numbers using if, if else and
nested if else statements.
Exercise 10. Write a program to check whether a year (integer) entered by the user is a leap year or
not.
Exercise 11. Write a program to check whether a number entered by the user is even or odd.
Exercise 12. Write a program to check to check whether a given number is positive or negative.
Exercise 13. Write a program to check whether a Triangle is Equilateral, Isosceles or Scalene

Introduction to Computing CO1005 – Semester 1 of 2019-2020 3/3

You might also like