Sei sulla pagina 1di 15

1 Hands-on Exercises/

Hands-on Exercises ________


Create, execute, edit a program.

Exercise 1. Write a program that will output “Hello World”. (5 points)

Exercise 2. Write a program that will output the quotation below. (5 points)

My formula for success?


Rise early, work late, strike oil.

Exercise 3. Write a program that will output the Great Quotation below. (5 points)

"It is a wise father that knows his own child." Shakespeare

Exercise 4. Write a program that will output the text below. (5 points)

Please
Be careful!!

Exercise 5. Write a program that will output the line of text below. (5 points)

Hi there!
This program is a bit longer than the others.
But really it’s only more text.
Hey, wait a minute!! What was that???
A bird?
A plane?
A control character?
And how will this look when it prints out?

Exercise 6. Write a program that will output your name and address using a
separate statement for each line of output. (5 points)

Exercise 7. Write a program to output the following text exactly as it appears here:

"It's freezing in here," he said coldly. (5 points)

Exercise 8. Write a program that prompts the user to enter a distance in meters and
then output that distance in yards, feet, and inches. (5 points)
2 Hands-on Exercises/

Exercise 9. Write a program that prompts the user to enter 5 integers (individually
entered) and then output their sum and product. (5 points)

Please enter an integer: 10


Please enter an integer: 5
Please enter an integer: 2
Please enter an integer: 3
Please enter an integer: 35

The sum is _______


The product is _______

Exercise 10. Write a program that prompts the user to enter a character and then
output that character 3 times on the screen. (5 points)

Please enter a character: A

AAA

Exercise 11. Write a program that prompts the user to enter his/her name and
address then output the name and address as shown in the example: (5 points)

Please enter your name: Juan


Please enter your address: Jaro, Iloilo City

Welcome to Jaro, Iloilo City Juan!

Exercise 12. Write a program that prompts for input of the length and width of a
rectangle, and then calculate and output the perimeter.
(5 points)
Exercise 13. Write a program that prompts the user to enter temperature in Celsius
and then convert it into Fahrenheit. (5 points)
Exercise 14. Write a program that prompts for the user’s weekly pay and the hours
worked to be entered through the keyboard as floating-point values. The program
should then calculate and output the average pay per hour in the following form:
(5 points)

Your average hourly pay rate is 7 dollars and 54 cents.

Exercise 15. Write a program that will calculate the total number of pets if there
are 2 cats, 1 dog, 1 pony and 46 other pets. The program will then display this
output: (5 points)
3 Hands-on Exercises/

“We have ____ pets in total.”

Exercise 16. Write a program that will calculate the total energy consumed in
calories if there are 5 cookies and each cookie has 125 calories. The program will
then display this output: (5 points)

“Total energy consumed is _____ calories”.

Exercise 17. Write a program that will calculate for the number of cookies left
over if there are 45 cookies in the jar and there are 7 number of children and each
child is given 6 cookies. The program will then display this output: (5 points)

“You have 7 children and 45 cookies”.


“Give each child 6 cookies”
“Ther are ____ cookies left over”.

Exercise 18. Write a program that will calculate for the length of a piece in feet if
the plank has a total length of 10 feet and to be cut in to 4 pieces. The program will
then display this output: (5 points)

“A plank 10 feet long can be cut into 4 pieces _____ feet long.

Exercise 19. Write a program that will calculate for the circumference and the area
of a table if the diameter of the table is equal to 6. The program will then display
this output: (5 points)

“The circumference is _____”


“The area is _____”

Exercise 20. Write a program that prompts the user to enter a single letter then
output the letter as shown below: (5 points)

Enter a single letter: ______


The letter is (letter)

Exercise 21. Write a program that prompts the user to enter his first name then
output the name as shown below: (5 points)

Enter your name: ______


Hello (your name)
4 Hands-on Exercises/

Exercise 22. Write a program that prompts the user to enter his/her age then output
the age as shown below: (5 points)

How old are you? _____


You are (age) years old.

Exercise 23. Write a program that prompts the user to input the length and width
of a room in feet and then calculates and outputs the floor area in square yard.
(5 points)

Enter the length of a room in feet: _______


Enter the width of a room in feet: ________
The floor area in square yard is _________

Exercise 24. Write a program that prompts the user to enter 4 integers
(individually entered) and then output their sum and average. (5 points)

Please enter the first integer: _____


Please enter the second integer: ______
Please enter the third integer: _______
Please enter the fourth integer: _______
The sum is _______
The average is ________.

Exercise 25. Write a program that prompts the user to enter a name and then
output that name 5 times on the screen. (5 points)

Please enter a name: Maria


MariaMariaMariaMariaMaria
5 Hands-on Exercises/

Hands-on Exercises ________


Use the if statement

Exercise 26. Write a program that will ask the user to enter his/her heart rate. If
his/her heart rate is greater than 56 print “Keep up your exercise program” else
print “Your heart is in excellent health”. (5 points)

Exercise 27. Write a program that will ask the user to enter his/her number of
years in the company. If the number of years is greater than 5 print “There is
bonus” else print “There is no bonus”. (5 points)

Exercise 28. Write a program that will ask the user to enter a number. If the
number is greater than or equal to zero print “Positive Number” else print
“Negative Number”. (5 points)

Exercise 29. Write a program that will ask the user to enter his/her grade given the
following condition: (5 points)
a. If grade is >=75 but not >100, print “You passed”
b. If grade is 100, print “You got a perfect score!”
c. If grade is <75, print “Sorry, you failed.”
d. If grade >100 or <65, then print “Invalid grade.”

Exercise 30. Write a program that will ask the user to guess a 4-digit pin code
given the following condition: (5 points)
a. If guess is less than 0 print “Positive numbers only”.
b. If guess is equal to pin code print “You are right”.
c. If guess is greater than the pincode print “ Wrong. That’s higher.”
d. Else print “ Wrong. That’s lower.”
6 Hands-on Exercises/

Hands-on Exercises ________


Use the if statement

Exercise 31. Write a program that will calculate grades A-F based on final
percentage score.
Final Percentage Score Letter Grade
95-100 A
90-94 A-
87-89 B+
84-86 B
80-83 B-
77-79 C+
74-76 C
70-73 C-
67-69 D+
64-76 D
60-63 D-
00-59 F

Exercise 32. Write a program that will ask the user to enter his/her age, then figure
out whether someone's a baby, toddler, child, adolescents, adulthood or old age.
AGE AGE GROUP
0-1 newborn
1-3 toddler
3-12 child
12-18 adolescents
19-65 adulthood
65 years and above old age

Exercise 33. Write a program that will find the largest/smallest of three numbers.

Exercise 34. Write a program that will ask the user to give a numerical date (year,
month, day), then display the date.
Exercise 35. Write a program that figure out whether a given year is a leap year.

Exercise 36. Write a program that will ask the user to give a number on the
roulette table, then figure out whether it's odd/even.
Exercise 37. Write a program that will test if a number is a multiple of 3, 5 or 7.
7 Hands-on Exercises/

Hands-on Exercises ________


Use the switch statement

Exercise 38. Design a program that will display the month corresponding to the
number entered by the user. For example, if the month number is 1, the program
should display the string “January”. If the month number is not 1 through 12,
display the “Incorrect month number” message.

Exercise 39. Karlton Learning wants a program that displays the amount of money
a company owes for a seminar. The fee per person is based on the number of
people the company registers, as shown below.

Number of registrants Fee per person


1 through 4 100 Pesos
5 through 10 80 Pesos
11 or more 60 Pesos

Exercise 40. Design a program that will display “Illinois” when the state code is
1, “Kentucky” when it’s 2, “New Hampshire” when it’s 3, “Vermont” when it’s 4.
and “Massachusetts” when it’s 5. If the state code is not 1 through 5, the code
should display the “Unknown state” message.

Exercise 41. Design a program that will ask the user to input a capital or small
letter, then display a word that begins with the letter inputted. If the inputted
character is not a letter, then display an error message. (5 points)

Example Output:
Enter a capital or small letter: __d__

“D is for dog”

Exercise 42. Write a program that will ask the user to enter his or her grade then
display his/her equivalent score. (5 points)

Grade: Score:
A 80-100
B 70-79
C 60-69
D 50-59
E 40-49
F 0-39

Example Output:
Enter your grade: __C__

“Your equivalent score is 60-69”


8 Hands-on Exercises/

Exercise 43. Write a program that will ask the user to enter the number of watts of
a light bulb then display its brightness. (5 points)

Watts Brightness
15 125
25 215
40 500
60 880
75 1000
100 1675

Example Output:
Enter the number of watts: __60__

“Brigthness is equivalent to 880”

Exercise 44. Create a program that will ask the user to input a wind category then
display its corresponding Wind speed. (5 points)

Wind Speed Category


72 Hurricane
55 Whole Gale
39 Gale
25 Strong Wind
10 Wind not strong

Exercise 45. Create a program that would ask the user to enter his/her age then
display his/her educational level. (5 points)

Age Educational Level


4 Playhouse
5 Kinder
6 Prep
7 Grade 1
8 Grade 2
9 Grade 3
10 Grade 4
11 Grade 5
12 Grade 6
13 First Year
14 Second Year
15 Third Year
16 Fourth Year

Example Output:
Enter your age: __15__

“Third Year”
9 Hands-on Exercises/

Exercise 46. Create a program that will ask the user to enter his/her membership
status and age. The membership variable contains one of the following
letters(entered in either uppercase or lowercase): M or N. The letter M stands for
member, and the letter N stands for non-member. The program should display the
appropriate seminar fee, which is based on a person’s membership status and age.

Seminar Fee Criteria


Php 100 Club member less than 65 years old
Php 50 Club member at least 65 years old
Php 500 Non-member

Exercise 47. Create a program that will ask an employee to enter his/her current
pay and his/her department (entered in either uppercase or lowercase): A, B, C, or
D. Employees in departments A and B will receive a 2% raise. Employees in
department C will receive a 1.5% raise, and employees in department D will
receive a 3% raise. Use the switch statement to calculate the appropriate raise
amount.

Exercise 48. Create a program that converts US Dollars to a different currency.


The currencies and exchange rates are listed below. The user should be allowed to
choose the currency. Designate a code for each currency and use cout statements to
display the code and corresponding currency. The number of American Dollars
should always be an integer that is greater than or equal to zero.

Currency Exchange Rate


Canada Dollar 1.01615
Eurozone Euro .638490
India Rupee 40.1798
Japan Yen 104.390
Mexico Peso 10.4613
South Africa Rand 7.60310
United Kingdom Pound .504285
10 Hands-on Exercises/

Hands-on Exercises ________


Use the if and switch statement

Exercise 49. Design a program that will display the employee’s salary corresponding to the
department code entered by the user. For example, if the department code is 1, the program
should display the salary “25000”. If the department code is 2, the program should display the
salary “30000”. If the department code is 3, the program should display the salary “32000”. If
the department code is not 1 through 3, display the “Invalid code. Salary=0” message.

Exercise 50. Design a program that will display the customer’s fee corresponding to the code
entered by the user. For example, if the code is S, the program should display the fee “40”. If the
code is F, the program should display the fee “50”. If the code is A, the program should display
the fee “30”. If the code is C, the program should display the fee “35”. If the code is not S,F.A, or
C, display the “Invalid code. Fee=0” message.

Exercise 51. The sales manager at Warren Company wants a program that displays a price based
on a product ID she enters. The valid IDs and their corresponding prices are shown below. If the
product ID is not valid, the program should display the “Invalid product ID” message.

Product ID Price
1 50.55
2 12.35
5 11.46
7 11.46
9 12.35
11 11.46

Exercise 52. Mr Jacoby teaches math at Kindlon High School. He wants a program that displays
a message based on a letter grade he enters. The valid letter grades and their corresponding
messages are shown below. If the letter grade is not valid, the program should display the
“Invalid Grade” message.

Letter Grade Message


A Excellent
B Above Average
C Average
D Below Average
E Below Average
11 Hands-on Exercises/

Practical Test ________


Use the if and switch statement

Test 1. Create a program that displays the Area and Perimeter of a shape(square or
rectangle). The program will get a letter(A for area and P for perimeter) and
another letter(S for square and R for Rectangle). The program will then ask the
user to enter the length and width of the shape and calculate its corresponding area
or perimeter. The formulas for calculating the area and perimeter of shape are
shown below. Save it as “If-Switch1.cpp” and run the program.

Formulas
Area Square A=Width * Width
Perimeter Square P=4 * Width
Area Rectangle A=Length * Width
Perimeter Rectangle P=2*Length+2*Width

Test Data
Properties Shape Width Length Answer
A S 2 4
P S 2 8
A R 3 2 6
P R 3 2 10
T Invalid

Test 2. Create a program that will calculate the diameter, circumference and area of
a circle. The program will need to get a letter (D for diameter, C for circumference,
A for Area) and will need to get the radius from the user. If the user enters an
invalid letter, the program should not ask the user for the radius. Instead, it should
display an appropriate error message before the program ends. The formulas for
calculating the diameter, circumference and area are shown below. Save it as “If-
Switch2.cpp” and run the program.

Formula
Diameter D=2* Radius
Circumference C=2*Pi*radius
Area A=Pi*Radius^2

Test Data
Properties Radius Output
D 2 4
C 2 12.5664
A 3 28.2744
E Invalid Letter
12 Hands-on Exercises/

Hands-on Exercises ________


Use loops statement (For,While,Do-While)

Exercise 53. Write a program that will ask the user to enter his/her name. The
program will then display his/her name 3 times. (5 points)

Example Output:
Enter your name: __Juan__
“Juan Juan Juan”

Exercise 54. Write a program that will ask the user to enter a letter. The program
will then print the letter 5 times. (5 points)
Example Output:
Enter a letter: __b__
“b b b b b”

Exercise 55. Design a program that will ask the user to input and display his/her
age. The loop will stop If his/her age is less than 0, the program will stop.
(5 points)

Example Output:
Enter your age: __3__
“You are 3 years old.”
Enter your age:__76_
“You are 76 years old.”
Enter your age:__-7_

Exercise 56. Design a program that will ask the user to if he/she wants to enter
his/her sales amount. If Yes then the program will display his/her sales and if No,
the program will end. (5 points)

Example Output:
Enter a sales amount (Y/N): __y__
Enter the sales: 10000
“You entered 10000”
Enter a sales amount (Y/N):__Y_
Enter the sales: 25000
“You entered 25000.”
Enter a sales amount (Y/N):__n_
13 Hands-on Exercises/

Exercise 57. The sales manager at Jasper Music Company wants a program that allows him to
enter the quarterly sales amount made in each of three regions: Region 1, Region 2, and Region
3. The program should calculate the total quarterly sales and then display the result on the screen.
The program will use a counter to ensure that the sales manager enters exactly three sales
amounts. It will use an accumulator to total the sales amounts. (5 points)
Example Output:
Enter region 1’s quarterly sales: __2500__
Enter region 2’s quarterly sales: __6000__
Enter region 3’s quarterly sales: __2000__
“Total quarterly sales: Php 10500.”

Exercise 58. The sales manager at Colfax Sales wants a program that allows him to enter a sales
amount. The program should calculate and display the appropriate commission using rates of
10%, 15%, 20%, and 25%. The program will use a counter to keep track of the four rates.(5 points)
Example Output:
Enter the sales: __25000__
“10% commission: Php2500”
“15% commission: Php3750”
“20% commission: Php5000”
“25% commission: Php6250”

Exercise 59. Professor Chang wants a program that allows him to enter a student’s project and
test scores. The professor assigns three projects and two tests. Each project is worth 50 points,
and each test is worth 100 points. The program should calculate and display the total points the
student earned on the projects and tests. It also should display the student’s grade, which is based
on the total points earned. (5 points)
Total points earned Grade
315.350 A
280.314 B
245.279 C
210.244 D
Below 210 F

Example Output:
1’s Project score:__45__
2’s Project score:__40__
3’s Project score:__41__
1’s Test score: __96__
2’s Test score: __89__
“Total points earned and grade: 311, B”
1’s Project score:__40__
2’s Project score:__35__
3’s Project score:__37__
1’s Test score: __73__
2’s Test score: __68__
“Total points earned and grade: 253, C”
1’s Project score: __-45_
14 Hands-on Exercises/

Exercise 60. Design a program that will ask the user to input a lower and a higher
number then the program will increment the lower number by 1 until it equals the
higher number. (5 points)

Example Output:
Enter a low number: __3__
Enter a high number:__9__
“3 4 5 6 7 8 9”

Exercise 61. Design a program that will ask the user to input a higher and a lower
number then the program will decrement the higher number by 1 until it equals the
lower number. (5 points)

Example Output:
Enter a high number: __9__
Enter a low number:__3__
“9 8 7 6 5 4 3”

Exercise 62. Design a program that will print all the odd numbers between 1 to 50.
(5 points)

Example Output:
“1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49”

Exercise 63. Design a program that will get the factorial of an inputted number.
(5 points)

Example Output:
Enter a number: __5__
“5*4*3*2*1= 120”

Exercise 64. Design a program that will input 10 integers and output: (5 points)
a. The total
b. The largest
c. The smallest
15 Hands-on Exercises/

Potrebbero piacerti anche