Вы находитесь на странице: 1из 7

Introduction to Computer Programming (MEng 1052)

Worksheet 1
1. Write a program that prompts the user for two integers and then prints the sum,
difference, product, average, distance, maximum, and minimum.
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.
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.
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.
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.]
6. Write a program to read in some integers, displaying the sum of those numbers after
doing some calculations. The program first asks you how many numbers you will be
working with. Your program then uses that value to count the number of times you go
through a loop. Each time through the loop you read in a number. If the number is even,
add it to the sum. If the number is odd, subtract it from the sum. For example, the user
could choose to process 3 numbers. The numbers entered could be 4 7 6. The sum starts
at 0. The first value (4) is added to the sum. The second value (7) is odd so it is
subtracted from the sum, giving -3 (the result of 4 - 7). The third value (6) is even, and
so is added to the sum, giving a result of 3, which is printed out. (Hint: you will need to
use the mod (%) operator.)
7. Write a complete C++ program that outputs the numbers 1 to 20, one per line. The
program does nothing else.
8. Write a program that prints out all multiples of 5 up until 100, separated by a space.
5 10 15 20 25 30 ... 100

1
9. Write a program that prints out all prime numbers less than 100, separated by a space.
10. 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 %.
11. 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,
12. 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.
13. Write a program that asks the user to enter a number N and then prints on the screen the
numbers 1 through N2 arranged in a NxN grid. An example is shown below, where the
user entered 4.

14. Write a C++ program that uses a while statement and the tab escape sequence
\t to print the following table of values:

15. Write a program that reads three nonzero integers and determines and prints whether
they’re the sides of a right triangle.
16. Write a program that reads three nonzero double values and determines and prints
whether they could represent the sides of a triangle.

2
17. The factorial of a nonnegative integer n is written n! (pronounced “n factorial”) and is
defined as follows:
n! = n · (n – 1) · (n – 2) · … · 1 (for values of n greater than 1) and
n! = 1 (for n = 0 or n = 1).
For example, 5! = 5 · 4 · 3 · 2 · 1, which is 120. Write a program that reads a nonnegative
integer and computes and prints its factorial.
18. Write a program that has main() call a user-defined function that takes a Celsius
temperature value as an argument and then returns the equivalent Fahrenheit value. The
program should request the Celsius value as input from the user and display the result, as
shown in the following code:
Please enter a Celsius value: 20
20 degrees Celsius is 68 degrees Fahrenheit.

For reference, here is the formula for making the conversion:


Fahrenheit = 1.8 × degrees Celsius + 32.0
19. Write a program that reads in two integers and determines and prints if the first is a
multiple of the second.
20. 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.

21. 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

3
22. Write a program that asks the user to enter two integers, obtains the numbers from the
user, and then prints the larger number followed by the words "is larger." If the
numbers are equal, print the message "These numbers are equal."
23. Write a program that inputs three integers from the keyboard and prints the sum, average,
product, smallest and largest of these numbers. The screen dialog should appear as
follows:

24. Write a program that calculates the squares and cubes of the integers from 0 to 10. Use
tabs to print the following neatly formatted table of values:

25. Write a program that asks the user to enter the number of seconds as an integer value(use
type long) and that then displays the equivalent time in days, hours, minutes, and
seconds. Use symbolic constants to represent the number of hours in the day, the number
of minutes in an hour, and the number of seconds in a minute. The output should look
like this:
Enter the number of seconds: 31600000
31600000 seconds = 365 days, 46 minutes, 40 seconds

4
26. A person invests $1000.00 in a savings account yielding 5 percent interest. Assuming that
all interest is left on deposit in the account, calculate and print the amount of money in
the account at the end of each year for 10 years. Use the following formula for
determining these amounts:
a = p (1 + r) n
where
P is the original amount invested (i.e., the principal),
r is the annual interest rate,
n is the number of years and
a is the amount on deposit at the end of the nth year.
Hint: Use #include<cmath>directive for standard math library and predefined
function pow to calculate power. e.g., pow(x, y) =xy.
cmath is the library where the definition of pow is located.
The screen dialog should appear as follows:

27. Write a program that prints the following shapes.

5
28. 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 xTc
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 one-degree increments. For example, if the user entered 0 as
the start temperature and 2 as the end temperature then the program should output:

29. Buoyancy is the ability of an object to float. Archimedes’ principle states that the buoyant
force is equal to the weight of the fluid that is displaced by the submerged object. The
buoyant force can be computed by:
Fb= V xy
Where Fb is the buoyant force, V is the volume of the submerged object, and y is the
specific weight of the fluid. If Fb is greater than or equal to the weight of the object, then
it will float, otherwise it will sink.
Write a program that inputs the weight (in Newtons) and radius (in meter)of a sphere and
outputs whether the sphere will sink or float in water. Use y = 9810 N/m3 as the specific
weight of water. The volume of a sphere is computed by (4/3)πr3.

30. Write a program that finds and prints all of the prime numbers between 3and 100. A
prime number is a number such that one and itself are the only numbers that evenly
divide it (e.g., 3, 5, 7, 11, 13, 17 …).
One way to solve this problem is to use a doubly nested loop. The outer loop can iterate
from 3 to 100 while the inner loop checks to see if the counter value for the outer loop is
prime. One way to see if number n is prime is to loop from 2 to n−1 and if any of these
numbers evenly divides n, then n cannot be prime. If none of the values from 2 to n−1
evenly divide n, then n must be prime.

6
31. Daphne invests $100 at 10% simple interest. That is, every year, the investment
earns10% of the original investment, or $10 each and every year:
interest = 0.10 × original balance
At the same time, Cleo invests $100 at 5% compound interest. That is, interest is 5% of
the current balance, including previous additions of interest:
interest = 0.05 × current balance
Cleo earns 5% of $100 the first year, giving her $105. The next year she earns 5% of
$105, or $5.25, and so on. Write a program that finds how many years it takes for the
value of Cleo’s investment to exceed the value of Daphne’s investment and then displays
the value of both investments at that time.

32. Write a program that uses a nested for loop to find and displays the factors of the
numbers from 2 to 100: The portion of output of the program should look like the
following.

33. Write a program and call it calc.cpp which is the basic calculator and receives there
values from input via keyboard.
a. The first value as an operator (Op1) should be a char type and one of (+, - , *, /)
b. Program should receive another two operands (Num1, Num2) which could be a
float or integer.
c. The program should apply the first operator (Op1) into the operands (Num1,
Num2) and prints the relevant results with related messages in the screen.
d. Use a do while loop to enable the user to use the program as long as he/she wants.

Вам также может понравиться