1. Computer Programming , C++
Worked Examples
Debre Markos University
Institute of Technology
Department of Mechanical Engineering
Compiled by: Simegnew Ataley
jasimegnew@gmail.com
December 2019 (2010 E.C)
Debre markos, Ethiopia
2. 1. Write a program that prompts the user for two integers and
then prints the sum, difference, product, average, distance,
maximum, and minimum.
Answer:
5. 2. Write a program which accepts days as integer and display total
number of years, months and days in it. For example: If user input as
856 days the output should be 2 years 4 months 6 days.
Answer
7. 3. Write a program to calculate the total expenses. Quantity and
price per item are input by the user and discount of 10% is
offered if the expense is more than 5000.
Answer:
10. 4. Any year is input by the user. Write a program to determine whether
the year is a leap year or not. A year is leap year if a. The year is
evenly divisible by 4, and b. The year is not evenly divided by 100 or
the year is evenly divisible by 400.
Answer
12. 5. A palindrome is a number or a text phrase that reads the same backward as
forward. For example, each of the following five-digit integers is a
palindrome: 12321, 55555, 45554 and 11611. Write a program that reads in a
five-digit integer and determines whether it’s a palindrome. [Hint: Use the
division and modulus operators to separate the number into its individual
digits.]
Answer
18. 8. Write a program that prints out all prime numbers less than
100, separated by a space.
output
19. 9. Write a complete C++ program that reads two whole numbers
into two variables of type int, and then outputs both the whole
number part and the remainder when the first number is divided by
the second. This can be done using the operators / and %
answer
20. 10. Write a while loop that prints the numbers from an integer N down
to zero, separated by commas. For example, N=4 prints: 4,3,2,1,0,
Answer
22. 11. Write a program that requests the user to enter two integers. The
program should then calculate and report the sum of all the integers
between and including the two integers. At this point, assume that the
smaller integer is entered first. For example, if the user enters 2 and 9,
the program should report that the sum of all the integers from 2
through 9 is 44.
answer
24. 12. Write a short program that asks for your height in meter and your
weight in kilograms. (Use two variables to store the information.) Have the
program report your body mass index (BMI). Compute your BMI by
dividing your mass in kilograms by the square of your height in meters. The
program should display the following messages based on the calculated
BMI.
BMI Weight Status
✓ Below 18.5 Underweight
✓ 18.5 – 24.9 Normal
✓ 25.0 – 29.9 Overweight
✓ 30.0 and Above Obese