Programming contests

20 Programming Exercise for Beginners

January 1, 2019 12:00 AM – December 31, 2019 12:00 AM

Convert Different Currencies to Hungarian Forints

Create a program that reads lines containing amounts expressed in U.S. dollars, G. B. Pounds, Euros, or Hungarian Forints from the standard input and converts it to Hungarian Forint with the following exchange rates:

  • 1 EUR = 322 HUF,
  • 1 GBP = 356 HUF,
  • 1 USD = 282 HUF.

Input Specification

Each input line consists of a positive integer, a space, and the currency: “EUR”, “GBP”, “HUF”, or “USD”.

The number of input lines are not restricted but the amounts in each line can be stored in a variable type int, and the grand total can be stored in a variable type long.

Sample Input

  1. 1 HUF
  2. 3 GBP
  3. 2 EUR
  4. 8 USD
  5. 5 GBP
download as text file

Output Specification

For each input line, print one line to the output containing the same amount expressed in Hungarian Forint. Finally print one extra line containing the grand total.

The output follows the format of the input except that the lines end with the word “HUF”.

Output for Sample Input

  1. 1 HUF
  2. 1068 HUF
  3. 644 HUF
  4. 2256 HUF
  5. 1780 HUF
  6. 5749 HUF
download as text file

Hints and Guide

This exercise can be easily solved by using the standard input-output library. While the scanf function supports formatted input, the printf function helps us create the output. Do not forget one big difference: after the pattern string, the scanf function requires memory addresses, not the value of a variable.

The exercise describes a strict input format. Note that there is no need to take care of the cases when the input does not follow its specification.

We can easily solve the exercise line by line. After reading an input line, for example with the help of the fgets function, we can use sscanf function to process the amount and the strcmp function to identify the currency.

One possible solution is available in: main.c.

Acknowledgement This work was supported by the construction EFOP-3.4.3-16-2016-00021. The project was supported by the European Union, co-financed by the European Social Fund.
University of Debrecen; Faculty of Informatics; v. 03/01/2019