Operators – Example Tasks (C, C++, Java)

Page 2 of 11 · Basic examples

About the examples

The examples practise

  • input and output conversion,
  • working with different data types,
  • variables, constants, symbolic constants,
  • the operations "+", "-", "*", "/",
  • the remainder of dividing two integers with the operator "%".
Before you start: read the lessons Operators in C, Operators in C++.
How to practise: first try to solve the task on your own (at least on paper), then open the hint, and the solution only at the end. The solutions are mostly in C or C++; if you are learning Java or Python, rewrite the idea in your language.
TASK 01

The remainder of integer division of two numbers

Enter two integers X and Y. Determine the result and the remainder of dividing X by Y.
Example:
Input
7 3
Output
2 1
Explanation:
7/3 = 2
7 % 3 = 1, which is the integer remainder of dividing the numbers 7 and 3.

TASK 02

Time in minutes and seconds

Enter a time in seconds, and then show it in minutes and seconds.

Example:
Input
135 //sec
Output
2 min: 15 sec
Explanation:
Since 1 min = 60 sec, 135 sec contain 2 min = 2*60 = 120 sec. The remaining time in seconds is 15 sec.
2 * 60 s + 15 s = 135 sec

TASK 03

The digits of a three-digit number

Make a program that extracts the digits from an entered three-digit integer and shows them on the screen.
Example
Input
478
Output
4
7
8

TASK 04

The digits in reverse order

Make a program that extracts the digits of an entered three-digit integer into separate variables and prints them in reverse order.
E.g. for the input 589, the output should be 985.

TASK 05

A tablecloth

Write a program that calculates the length of tape needed to hem a square tablecloth whose area is P.

TASK 06

A present

Every present contains exactly k chocolate bars. If we have n chocolate bars in total, how many presents can be packed?

TASK 07

Building with bricks

Jova builds toys from lego bricks. He wants to build one car, for which he needs 4 wheels, 8 large and 4 small bricks, and one bicycle, for which he needs 2 wheels and 6 small bricks. If a wheel costs 79 dinars, a small brick 59, and a large brick 99 dinars, write a program that calculates how many dinars Jova needs to buy all the parts he needs.
One possible solution of the task is to calculate the result by forming one compound expression.

Source (in Serbian): petlja.org/biblioteka/r/lekcije/prirucnik-python/izracunavanje-cas7#id2

TASK 08

The best jump

Two long jump competitors compete, and the winner is the one with the best result in 3 jumps. Print the value of the winner's best jump, for the entered values of the jumps during the competition.
Input: 6 real numbers (0 - 9), where the first 3 are the jumps of the first competitor in three rounds, and the last 3 numbers are the jumps of the second competitor in 3 rounds.
Output: One real number that is the winner's best jump.
Note: Solve the task by using the operator "?:"

Example
Input: 7.8 7.5 7.77 6.9 8.0 7.97
Output: 8.0

TASK 09

Converting a data type

Enter a real number and show its integer part. Use casting, that is, converting the data type in the program.
E.g. for the entered value 60.89, show the value 60 in the output.

TASK 10

Converting an angle

An angle in degrees with two decimals is given. Write a program that expresses the given angle in degrees and minutes. For example, show 60.75 as 60 degrees and 45 minutes.

TASK 11

A fraction as a mixed number

a) The numerator of a fraction is 37, and the denominator is 12. Convert this fraction into a mixed number.
Source (in Serbian): petlja.org/biblioteka/r/lekcije/prirucnik-python/izracunavanje-cas8

b) Do the previous example so that the user enters the value of the numerator a and the denominator b of the fraction.

TASK 12

Converting time

Read a time in seconds and print it in the form hh:mm:ss.

TASK 13

The angle of the hour hand

Source (in Serbian): petlja.org/biblioteka/r/Zbirka/ugao_satne_kazaljke
Given the angle that the hour hand makes with the vertical half-line joining the centre and the top of the clock (the number 12), determine the time in hours and minutes.

Input
An integer that is the angle in degrees is read from the standard input.

Output
Print two integers h (0≤h<12) and m (0≤m<60) to the standard output; they are the number of hours and minutes closest to the position of the hour hand.

Example
Input
90
Output
3:0
Example 2
Input

200
Output
6:40

TASK 14

The end of lessons

Today your first lesson at school starts at 08:00 AM. One lesson lasts 45 minutes, a short break between lessons lasts 5 minutes and the long break lasts 20 minutes. The long break is after the 2nd lesson, and the short breaks are after all the others.
If you have N lessons today (and you will surely have more than 2), what time will it be (hours and minutes) when your last lesson ends?

TASK 15

The hundreds digit and the tens digit

Write a program that takes the tens digit and the hundreds digit out of the decimal notation of a number. E.g. for the entered value 4679, extract 6 and 7 and show them on the screen.

TASK 16

A railing

Source (in Serbian): petlja.org/biblioteka/r/Zbirka/ograda_terase
On a terrace d metres long, n posts s centimetres wide should be arranged so that the distance between the posts, and between a post and the wall, is the same.

Input

  • d - the length of the terrace in metres (1≤d≤20)
  • n - the number of posts (1≤n≤100)
  • s - the width of a post in centimetres (5≤s≤15)

Output
The distance between the posts in centimetres, rounded to two decimals.
Example
Input
10
30
10
Output
22.58

TASK 17

Tic-tac-toe

Source (in Serbian): petlja.org/biblioteka/r/Zbirka/iks_oks

Mirko is a young programmer who is trying to program the game tic-tac-toe. He is close to finishing, but he needs a little help. He decided that the user chooses with the mouse the square in which their symbol will be written. The playing field consists of 9 squares (arranged in three rows and three columns) and every square is 100 by 100 pixels (the whole field is 300 by 300 pixels). The position of the pixel that was clicked with the mouse is known, and you need to determine the number of the square in which that pixel is. The position of the pixel is determined by its ordinal numbers (coordinates) horizontally and vertically, counting from the lower left corner of the field (the pixels are counted from 1 to 300). The squares are numbered from 1 to 9, row by row, starting from the lower left corner of the field, as shown in the following picture:

789
456
123

Input
Two integers x and y (1≤x,y≤300), each in its own line, are read from the standard input; they are the x and y coordinates of the pixel that was clicked with the mouse.

Output
One number from 1 to 9 that is the number of the square is printed to the standard output.

Example 1

Input
1
1
Output
1
Example 2
Input

120
280
Output
8
Example 3
Input
100
201
Output
7
Example 4
Input
101
300
Output
8

TASK 18

Monopoly

Source (in Serbian): Petlja, Aritmetika.CelobrojnoDeljenje.ModularnaAritmetika

Monopoly is a game in which the players move along fields arranged in a circle. The players always move clockwise. Suppose that the fields are marked with ordinal numbers starting from 0, that at the beginning of the game both players are on that field and that during the game the players have not moved backwards. If we know the number of fields the first player has passed since the beginning of the game and the number of fields the second player has passed since the beginning of the game, write a program that calculates how many steps the first player needs to make to come to the field where the second player is.

Input
Three natural numbers are entered from the standard input. The first line gives the number of fields on the board, the second the number of fields the first player has passed since the beginning of the game, and the third the number of fields the second player has passed since the beginning of the game.
Output
Print to the standard output how many steps forward the player needs to make to reach the wanted field.
Example 1
Input
10
3
7
Output
4
Example 2
Input
10
7
3
Output
6

Topics: arithmetic operations, logic, div, mod

TASK 19

Runners

Two runners run on a circular track of length s [km], one at the speed of v1 [km/h], the other at the speed of v2 [km/h]. How much time will the faster runner need to overtake the slower one by a whole lap?

Input
Data with two decimals:

  • s - the length of the circular track (0≤s≤10)
  • v1 - the speed of the first runner (1≤v1≤45)
  • v2 - the speed of the second runner (1≤v2≤45)

Output
The integer values of the number of hours, minutes and seconds that are the time the faster runner needs to catch up with the slower one by a whole lap. Show the seconds rounded to the nearest integer.

Example
Input
10.00
5.00
6.00
Output
10
0
0

TASK 20

A chessboard

On a chessboard of width n and length m, the upper left field is white. Write a program that determines the number of black fields on the given chessboard.

TASK 21

Conversion

Mrs Pufna likes to play games, so she has stored a lot of computer games on the hard disk of her computer. But Mrs Pufna also wants to save a song by her favourite band on the computer, so she must calculate how much space there is on the hard disk after she has saved the song, which is n minutes and m seconds long. It is known that the remaining free space on the hard disk is k MB (megabytes), and that recording 1 second of sound needs 16 KB (kilobytes). Write a program CONVERSION that prints (in one or more lines) two numbers: how many
MB and how many KB of space are left on the hard disk. Remember that 1 MB = 1024 KB (1 megabyte has 1024 kilobytes). The only line of the standard input contains the integers n, m, k (1<= n, m, k <=1000000), separated from each other by one space. Assume that the final result will always be non-negative numbers
(numbers that are not less than 0).
Note: A task from the municipal competition, season 2015/2016, dms.rs/pripreme-informatika/

Example
Input
5 28 32

Output
26 896

Explanation of the test example: n=5 minutes, m=28 seconds, k=32 MB
5 minutes 28 seconds = 328 seconds (and 328 seconds need 5248 KB of memory)
Before recording the song there were k=32 MB = 32768 KB on the hard disk
After Pufna records the song, 32768 KB − 5248 KB = 27520 KB will be left
When we convert that number into MB and KB it will be: 26 MB and 896 KB

TASK 22

A bridge

Prince Ljubinko decided to build a bridge over the river in his home town. He gathered the best master builders, who started building a stone bridge. In the quarries they cut huge stone slabs of equal size, which they placed on pillars, and that was the base of the bridge. The width of a slab always determined the width of the bridge. So that the edges of these slabs would not get damaged over time, after laying the slabs in a row they placed small square slabs, 1 metre on each side, around these large slabs. Write a program BRIDGE in which the width of a slab S and the length of a slab D, given in metres, and the number of slabs N needed to make the base of one bridge are entered, and then
the number of small square slabs needed along the edges of the bridge is calculated.
The values of the variables S, D and N are positive integers, 0 < S, D, N ≤ 100

Example. Input: S = 5 D = 7 N = 3 Output: 56

Note: A task from the municipal competition, season 2009/2010, dms.rs/pripreme-informatika/

TASK 23

A book

Note: A task from the first round of qualifications, season 2019/2020, dms.rs/pripreme-informatika/

A book has N chapters. On the first day Nikola read A chapters, and on the second day two chapters more than on the first. Write a program that reads the positive integers N, A and prints how many chapters Nikola still has to read.
Input: The first line of the standard input contains the number N (1 ≤ N ≤ 99), and the second line the number A (1 ≤ A ≤ 99). The input data are such that the remaining number of chapters is never negative.
Output: Print one number to the standard output, the number of chapters Nikola has not read yet.
Example 1
Input

15
2
Output
9
Example 2
Input
20
9
Output
0

TASK 24

A light bulb

Note: A task from the first round of qualifications, season 2019/2020, dms.rs/pripreme-informatika/

The street lamp posts are numbered in order, street by street. Every street has N posts, so the posts in the first street are numbered 1, 2, … N, in the second street the posts have the numbers N + 1, N + 2, … 2N, etc. Mirko got the job of replacing the light bulb on every third post in every second street. When he came to the post with the number A, Mirko lost count. Write a program that reads the positive integers N and A and answers the question whether the light bulb on that post should be replaced.

Read the lesson on this topic on the page Data in C/C++.

Common mistakes and tips
  • You extract the digits of a number by combining / and %: the units n % 10, the tens n / 10 % 10, the hundreds n / 100.
  • The operator % works only with integers. The remainder of a negative number in C/C++ is negative (-7 % 3 is -1).
  • Converting units (seconds into minutes, degrees into minutes) always follows the same pattern: the integer quotient + the remainder.

Check your knowledge

Open the quiz

Mini quiz: operators

1. How do you get the tens digit of the number n = 478?




2. What is 135 % 60?




3. What is the value of x after int x = 5; x += 3;?