Work Sheet 3 - Repetition Flow Controls 60070
Work Sheet 3 - Repetition Flow Controls 60070
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
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.
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.
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".
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.
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
----------------------------------------------------------------------------------------------------------------------
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.
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.
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
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
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.
******* 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
***** * *
(j) (k) (l) **
**** **
***
*** ***
****
** **** *****
* ***** ******
** **** *****
*** *** ****
**** ***
** **
***** * *
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
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
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.
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:
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.
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.
a) b)
b)
if ( age >= 65 )
cout << "Age is greater than or equal to 65" << endl;
else;
cout << "Age is less than 65 << endl";
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;
}