0% found this document useful (0 votes)
245 views13 pages

Work Sheet 3 - Repetition Flow Controls 60070

This document contains 23 practice questions on repetition flow controls in C++. The questions cover a range of topics including counting positive and negative numbers in a list, generating multiplication tables, printing patterns using loops, checking for Armstrong, perfect and strong numbers, calculating sums and products of series, separating number digits, checking for palindromes, and more. Students are asked to write C++ code to solve each problem, choosing the appropriate looping statement such as for, while, or do-while loops.

Uploaded by

Fares Belayneh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
245 views13 pages

Work Sheet 3 - Repetition Flow Controls 60070

This document contains 23 practice questions on repetition flow controls in C++. The questions cover a range of topics including counting positive and negative numbers in a list, generating multiplication tables, printing patterns using loops, checking for Armstrong, perfect and strong numbers, calculating sums and products of series, separating number digits, checking for palindromes, and more. Students are asked to write C++ code to solve each problem, choosing the appropriate looping statement such as for, while, or do-while loops.

Uploaded by

Fares Belayneh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Addis Ababa Science and Technology University

College of Electrical and Mechanical Engineering


Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

Work Sheet 3 - Practical Exercise Questions on


Repetition Flow Controls
Part I – General program writing exercises
1. For each of the following problems write a C++ code to perform the required task.
a) Counts the number of positive and negative values found within list numbers
b) Displays the multiplication table in a proper output format
2. Write a C++ program to print the following - using while loop
a) All natural numbers from 1 to n.
b) All natural numbers in reverse (from n to 1).
c) All alphabets from a to z.
d) All even numbers between 1 to 100.
e) All odd number between 1 to 100.
3. Write a program that displays N numbers, where N is a positive number to be entered from
the keyboard. Use proper mechanisms of validating appropriate input entry.

a. Starting from zero.


b. Starting from one.
c. Starting from a number entered from the keyboard.
4. Write a program to perform the following required tasks:
a. Check if a number is Armstrong or not. And also print all Armstrong numbers
between 1 to n.
b. Check whether a number is Perfect number or not. And also print all Perfect
numbers between 1 to n.
c. Check whether a number is Strong number or not. And also print all Strong numbers
between 1 to n.
5. Write a program that generate the following table
1990 135
1991 7290
1992 11300
1993 16200

Fundamentals of Programming Page 1


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

6. Write an application to print out the numbers 10 through 49 in the following manner

10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49

7. Write a program that continues to ask the user to enter any number other than 5 until the user
enters the number 5. Then tell the user “Hey! You weren’t supposed to enter 5!” and exit the
program.

8. Write a program to calculate both the square and cube of numbers from 1 to 10
1 1 1
2 4 8
3 9 27
. . .
. . .
10 100 1000
9. Write a C++ program to find the sum of
a) 1st hundred natural number
b) all natural numbers between 1 to n.
c) all even numbers between 1 to n.
d) all odd numbers between 1 to n.
10. Write for, do-while, and while statements to compute the following sums and products
according to the given series.
a. 5+10+15+…+50
b. 1*2*3*…*20
c. 1+2+3+…+100
d. 1+1/2+1/3+1/4+…1/15
e. 1 + 2 + 3 + 4 + . . .+ n
f. 1-1/2+1/3+ . . . +1/999
g. 1-1/2+1/3+1/4+ . . . . +1/99+1/100
h. 1-1/2! + 1/3! + 1/4! + . . . . +1/99! + 1/100!
i. 1 - 33 + 53 + 73 +...... (-1)n (2n + 1), n=333

Fundamentals of Programming Page 2


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

11. Write a C++ code that computes the sum of the following series. The program should accept
n from the user.
Sum = 1! + 2! + 3! + 4! + …n!

12. Write a program that computes and displays the sum of the reciprocals of n positive numbers,
where n is positive integer number to be entered from the keyboard.

Sum = 1+1/2+1/3+ . . . +1/n.

13. Compute the natural logarithm of 2, by adding up to n terms in the series


1 - 1/2 + 1/3 -1/4…1/n.

14. Design a program that read ten integers from keyboard in the range 0 to 100 and count how
many of them are larger than 50 and display the result.

15. Write an application that inputs a number from the user and separates the number into its
individual digits and prints the digits separated from one another by two spaces each. For
example, if the user types in the number 42339, the program should print 4 2 3 3 9.

16. Write a program that accepts an integer from the user and
a. Counts the number of digits in an integer number (E.g. 23,498 has five digits)
b. Prints the reverses the number (E.g. input = 4637215 Output=5217364)
c. Prints the first and the last digit of the entered integer.

d. Finds the sum of first and last digit of a number


e. Swaps the first and the last digits of a number
f. Compute the sum of digits of a number
g. Compute the product of digits of a number
h. Check whether a number is palindrome or not.
i. Find frequency of each digit in a given integer.
j. Sort the digits of a given number only using loops. (E.g. input = 4637215
Output=1234567)
17. Write a program that accepts a positive integer and print it in words.

18. Write a program that read and checks if the given year is Leap Year. If yes, prints the next 20
leap years otherwise a message that says "Not Leap Year".

Fundamentals of Programming Page 3


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

19. Write a weather-calculator program that asks for a list of the previous 10 days’ temperatures,
computes the average, and prints the results. You have to compute the total as the input
occurs, then divide that total by 10 to find the average. Use a while loop for the 10
repetitions.
20. An online retailer sells five products whose retail prices are as follows:
Product 1, $2.98;
Product 2, $4.50;
Product 3, $9.98;
Product 4, $4.49
Product 5, $6.87.

Write an application that reads a series of pairs of numbers as product number and quantity
sold. Your program should use a switch statement to determine the retail price for each
product. It should calculate and display the total retail value of all products sold. Use a
sentinel-controlled loop to determine when the program should stop looping and display the
final results.

(Misc. Application) a. An old Arabian legend has it that a fabulously wealthy but unthinking
king agreed to give a beggar one cent and double the amount for 64 days. Using this
information, write, run, and test a C ++program that displays how much the king must pay
the beggar on each day. The output of your program should appear as follows and also
determine on which day the king will have paid the beggar a total of one million dollars.

Day Amount Owed


------ -------------------
1 0.01
2 0.02
3 0.04
......
64
b. Modify the program you wrote for Exercise 10a Write a C++ program to find one's
complement and two's complement of a binary number.

21. Make a virus using while loop the virus should run in a windows background. Write the
antivirus for the above virus.
Fundamentals of Programming Page 4
Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

Part II (Choosing among looping statement)


Instruction: – Choose the most appropriate looping statement to solve the following problems
and write a program based on the selected looping statement.

22. Write a C++ program that allows the user to input N numbers and compute their average and
after each entry it should display the average. Terminate when 0 is pressed.

23. Write a program that displays the maximum and the minimum of a list of numbers entered
from the keyboard. Where the size of the list is to be entered from the keyboard.

24. Write a program that displays the first N positive integer numbers in reversed (descending)
order where N is a positive number to be entered from the keyboard.

25. Write a program that lets a user to enter numbers as long as she/he has entered a positive
number. When the user enters a negative number, the program must display a message that
says ‘Now you have entered a negative number’ and then the program should be terminated.

26. Write a program that displays the upper and the lower cases of each of the Latin alphabets as
shown in the following example.

Example: Aa Bb Cc Dd . . . Zz.

27. Write a program that computes and displays the sum, the difference and the product of a
range of consecutive numbers where the starting and ending numbers of the range are to be
entered from the keyboard.

28. Write a program that computes and displays the square root each of the numbers
entered from the keyboard. Once a number is entered and its square root is displayed, the
program should only continue repeating the same task if the user is willing to do.

Remark:- Make an appropriate data entry validation

29. Write a program that calculates and displays the factorial of n where n is a positive integer
number to be entered from the keyboard.
Example n! = n * (n-1) * (n-2) * . . . * 2 * 1
1! = 0! = 1

30. Write a program accepts list of marks of students and counts the number of students who
have scored marks greater than 20 in an exam out of 30. The total number of students who
took the test is to be entered from the keyboard.

Fundamentals of Programming Page 5


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

31. Write a C++ program that accepts marks of five students and then displays their average. The
program should not accept mark which is less than 0 and mark greater than 100.

32. Write a C++ application that can compute the letter grade of a student after accepting the
student’s mid and final mark. The program should only accept mid result [0-40] and final [0-
60]. If the data entered violates this rule, the program should display that the user should
enter the mark in the specified range.
a. The program that calculates the letter grades of 20 students
b. The program is will expected to run until the user refuses to continue.

33. Design a progam to compute the weekly average rainfall of Addis Abaab city given that the
daily rainfall for four weeks.
34. The arithmetic mean of two numbers is the result of dividing their sum by two. The
geometric mean of two numbers is the square root of their product. Write a program that
takes two floating point numbers as input and displays these two means
35. A prime number is an integer greater than one and divisible only by itself and one. The first
seven prime numbers are 2, 3, 5, 7, 11, 13, and 17. Write a program that

a. Accepts a number and check whether a number is prime or not


b. Displays all the prime numbers between 1 and 100.
c. Compute sum of all prime numbers between 1 to n.
d. Find all prime factors of a number.
36. Develop a C++ Program to check whether a number can be expressed as sum of two Prime
numbers.
37. Write a program that compute and displays the LCM and the GCF.

38. Write a C++ program to print all ASCII character with their values.

39. Using the ASCII table numbers, write a program to print the following output, using a nested
for loop. (Hint: the outer loop should loop from 1 to 5, and the inner loop’s start variable
should be 65, the value of ASCII “A”).

A
AB
ABC
ABCD
ABCDE

Fundamentals of Programming Page 6


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

40. Display the following output using their ASCII values:-


A
Bc
Def
Ghij
Klmno
pqrstuvw

41. Write a C++ program to print Pascal triangle up to n rows.

42. Write an application that prompts the user to enter the size of the side of a square, then
displays a hollow square of that size made of asterisks. Your program should work for
squares of all side lengths between 1 and 20.

43. Print the block letter "B" in a 7 x 6 grid.


44. Write separate programs that generate each of the following patterns.

******* ABCDE F 1 2 3
(a) ******* (b) GH I JK L (c) 4 5 6
******* MNOPQR 7 8 9
******* STUVWX

* * ******
(d) ** (e) ** (f) *****
*** *** ****
**** **** ***
***** ***** **
****** ****** *

abcdef f
(g) a (h) (i)
abcde ef
ab
abcd def
abc
abc cdef
abcd
ab bcdef
abcde
a abcdef
abcdef

Fundamentals of Programming Page 7


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

***** * *
(j) (k) (l) **
**** **
***
*** ***
****
** **** *****
* ***** ******
** **** *****
*** *** ****
**** ***
** **
***** * *

(m) abcdefg (n) (o) g


a
abcdef fg
bc
abcde efg
de f
abcd defg
gh i j
abc defg
kl mno
ab bcdefg
pq r stu
a abcdefg

1 12345 12345
(p) 12 (q) 2345 (r) 1234
123 345 123
1234 45 12
12345 5 1

1******
5 543212345
1 2*****
(s) (t) 545 (u) 5432345
123****
1234*** 54345 54345
12345** 5432345 545
123456* 543212345 5
1234567

Fundamentals of Programming Page 8


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

5 543212345 5
(v) (x) (y)
454 5432345 454
34543 54345 34543
2345432 545 2345432
123454321 5 123454321
2345432 545 2345432
34543 54345 34543
454 5432345 454
5 543212345 5

45. Write a program that prints a box, an oval, an arrow and a diamond as follows

46. Write a program to display the character W and M like this:

Fundamentals of Programming Page 9


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

Part III – Case Study


47. Workers at a particular company have won a 7.6% pay increase retroactive for 6 months.
Write a program that takes an employee’s previous annual salary as input, and outputs the
amount of retroactive pay due the employee, the new annual salary, and the new monthly
salary. Use a variable declaration with the modifier const to express the pay increase. Your
program should allow the calculation to be repeated as often as the user wishes.

48. A company pays an annual bonus to its employees. The bonus is calculated by multiplying
the employee’s annual salary by a bonus rate, which is based on the number of years the
employee has been with the company. Employees working at the company for less than 5
years receive a 1% bonus; all others receive a 2% bonus. Write two versions of an algorithm
that prints each employee’s bonus.

49. Professor Beyene Peteros 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. Shown below is the grading scale that Professor
Chang uses when assigning grades.

Total points earned Grade


315 – 350 A
280 – 314 B
245 – 279 C
210 – 244 D
below 210 F
50. Write a program that reads in ten whole numbers and that outputs the sum of all the numbers
greater than zero, the sum of all the numbers less than zero (which will be a negative number
or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters
the ten numbers just once each and the user can enter them in any order. Your
program should not ask the user to enter the positive numbers and the negative numbers
separately.

Fundamentals of Programming Page 10


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

51. An employee is paid at a rate of $16.78 per hour for the first 40 hours worked in a week. Any
hours over that are paid at the overtime rate of one and-one-half times that. From the
worker’s gross pay, 6% is withheld for social security tax, 14% is withheld for federal
income tax, 5% is withheld for state income tax, and $10 per week is withheld for union
dues. If the worker has three or more dependents, then an additional $35 is withheld
to cover the extra cost of health insurance beyond what the employer pays. Write a program
that will read in the number of hours worked in a week and the number of dependents as
input and will then output the worker’s gross pay, each withholding amount, and the net take-
home pay for the week.

52. Sound travels through air as a result of collisions between the molecules in the air. The
temperature of the air affects the speed of the molecules, which in turn affects the speed of
sound. The velocity of sound in dry air can be approximated by the formula:

Velocity ≈ 331.3 + 0.61 × Tc

where Tc is the temperature of the air in degrees Celsius and the velocity is in
meters/second.

Write a program that allows the user to input a starting and an ending
temperature. Within this temperature range, the program should output the temperature
and the corresponding velocity in 1° increments. For example, if the user entered 0 as the
start temperature and 2 as the end temperature, then the program should output.

53. Write a program that calculates the total grade for N classroom exercises as a percentage.
The user should input the value for N followed by each of the N scores and totals. Calculate
the overall percentage (sum of the total points earned divided by the total points possible)
and output it as a percentage. Sample input and output is shown below.

How many exercises to input? 3


Score received for exercise 1: 10
Total points possible for exercise 1: 10
Score received for exercise 2: 7
Total points possible for exercise 2: 12
Score received for exercise 3: 5
Total points possible for exercise 3: 8
Your total is 22 out of 30, or 73.33%.

Fundamentals of Programming Page 11


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

54. Holy digits Batman! The riddler is planning his next caper somewhere on Addis Ababa
Avenue. In his usual sporting fashion, he has left the address in the form of a puzzle. The
address on Addis Ababa Avenue is a four-digit number where:
 All four digits are different
 The digit in the thousands place is three times the digit in the tens place
 The number is odd
 The sum of the digits is 27
Write a program that uses a loop (or loops) to find the address where the riddle plans to strike.

Part IV – Output Prediction and Correcting Program Errors


1. What will be the output of the following programs

a) b)

2. Identify and correct the error(s) in each of the following:


a)
if ( age >= 65 );
cout << "Age is greater than or equal to 65" << endl;
else
cout << "Age is less than 65 << endl";

b)
if ( age >= 65 )
cout << "Age is greater than or equal to 65" << endl;
else;
cout << "Age is less than 65 << endl";

Fundamentals of Programming Page 12


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

c)
int x = 1, total;
while ( x <= 10 )
{
total += x;
++x;
}

d)
While ( x <= 100 )
total += x;
++x;

e) while ( y > 0 ) {
cout << y << endl;
++y;
}

Fundamentals of Programming Page 13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy