Combined Tasks: Data, Branching and Loops

Collection 4 of 5 · Additional examples with solutions
Before you start: the lessons Branching in a program, Loops, Nested loops; easier tasks from this area are on the page Nested loops – examples.
How to practise: solve the task on your own, run the program on the example from the text, and only then open the solution and compare the ideas.

This section gives example tasks with solutions from the areas of data, branching in a program, loops and nested loops. Try to solve the examples on your own, and use the solution to check your work, or if you still cannot solve the task after many attempts and a lot of effort.
The solution is shown by clicking the button next to the task.
The difficulty of each task is written next to it (top right): easy, medium or harder.

Tasks for preparing for a test:

TASK 01Difficulty: medium

Sweets

A computer science teacher wants to share candies among her students. Write a program SWEETS that reads the number of candies the teacher has and the number of students, and prints the smallest number of candies she needs to buy so that every student gets the same number of candies.

Example 1
Input: 234 24
Output: 6

Example 2
Input: 100 25
Output: 0

Source: municipal competition 2018.

TASK 02Difficulty: harder

Swapping digits

Write a program that swaps the tens digit and the hundreds digit in a given natural four-digit number. Then a three-digit number is made from the last three digits (on the right) of the new four-digit number.
For a number with fewer than four digits we consider the missing digits to be 0.

Input
The first line of the standard input contains a natural four-digit number.

Output
The three-digit number obtained after swapping the tens digit and the hundreds digit.

Example
Input
2349
Output
439

See a similar example: task 4 in the collection Additional tasks – data

TASK 03Difficulty: easy

The order of numbers

Enter three integers and check whether the second one is the smallest, and if it is, determine the final order (from the smallest to the largest).
A similar task: "The order of numbers" on the page Branching in a program – examples

TASK 04Difficulty: easy

The difference

Write a program that reads a three-digit number from the input and calculates the difference between the largest and the smallest three-digit number written with the same digits as the number that was read.

Examples (input → output): 123 → 198, 174 → 594, 505 → 45

TASK 05Difficulty: medium

Converting time

Read a time in minutes and seconds and print it in the form hh:mm:ss.
A similar task (the time given in seconds) is task 10 on the page
Additional tasks – data

TASK 06Difficulty: harder

Interval

Pera and Ana work in the same software company. If the working hours of each of them are known, write a program that determines how much time they spent at work together. The input contains the time when Pera came to work, the time when he left, the time when Ana came to work and the time when she left. Each time is given in its own line, as two numbers separated by one space. Print the result as a number of hours and minutes, again separated by one space.
Example
Input:
8 50
16 40
9 20
14 25
Output:
5 5
(Municipal competition 2018)

TASK 07Difficulty: medium

A rhombus of stars

A task from the website petlja (in Serbian):
petlja.org/biblioteka/r/Zbirka/romb_od_zvezdica

Write a program that prints a rhombus made of stars (as shown in the example).

Input
A natural number n (3≤n≤20) is entered from the standard input; it is the size of the rhombus (the number of rows and columns).

Output
Draw the rhombus to the standard output.

Example

Input

5

Output

    *****
   *****
  *****
 *****
*****

More branching tasks

These tasks used to be on this page too; now they are only in the collection of branching tasks:

Check your knowledge

Open the quiz

Mini quiz: combined tasks

1. How many candies need to be bought if there are 234 of them and 24 students?




2. What is the smallest three-digit number made of the digits 5, 0 and 5?




3. What are 135 minutes and 20 seconds in the format hh:mm:ss?