Some time ago, Peter bought bitcoins. Now, he is going on vacation in Europe and he needs Euro. Apart from bitcoins, he has Chinese yuans as well. Peter wants to exchange his money for Euro for the tour. Write a program that calculates how much Euro he can buy, depending on the following exchange rates:
- 1 bitcoin = 1168 leva.
- 1 Chinese yuan = 0.15 dollars.
- 1 dollar = 1.76 leva.
- 1 Euro = 1.95 leva.
The exchange office has commission fee within 0 to 5 percent from the final sum in Euro.
Three numbers are read from the console:
- On the first line – number of bitcoins. Integer within the range of [0 … 20].
- On the second line – number of Chinese yuans. Floating-point number within the range of [0.00 … 50 000.00].
- On the third line – commission fee. Floating-point number within the range of [0.00 … 5.00].
Print one number on the console – the result of the exchange of currencies. Rounding is not necessary.
- 1 bitcoin = 1168 leva
- 5 Chinese yuan = 0.75 dollars
- 0.75 dollars = 1.32 leva
- 1168 + 1.32 = 1169.32 leva = 599.651282051282 Euro
- Commission fee: 5% of 599.651282051282 = 29.9825641025641
- Result: 599.651282051282 - 29.9825641025641 = 569.668717948718 Euro
Let's first think of the way we can solve the task again, before having started to write code.
We see that the number of bitcoins and the number of Chinese yuans will be given in the input. The output should be in Euro. The exchange rates that we have to work with are specified in the task. We notice that we can only exchange the sum in leva to Euro, therefore, we first have to calculate the whole sum that Peter has in leva, and then calculate the output.
As we have information for the exchange rate of bitcoins to leva, we can directly exchange them. On the other hand, in order to get the value of Chinese yuans in leva, first we have to exchange them in dollars, and then the dollars to leva. Finally, we will sum the two values and calculate how much Euro that is.
Only the final step is left: calculating the commission fee and subtracting the new sum from the total one. We will obtain an integer for the commission fee, which will be a particular percent from the total sum. Let's divide it by 100, so as to calculate its percentage value and then multiply it by the sum in Euro. We will divide the result from the same sum and print the final sum on the console.
Bitcoins are given as an integer, therefore, we can declare a variable of
int type for their value. For Chinese yuan and commission fee we obtain a floating-point number, therefore, we are going to use
double is the data type with bigger scope, and the output should also be a floating-point number, we will use it for the other variables we create as well.
After we have build an idea on how to solve the task and we have chosen the data structures that we are going to use, it is time to get to writing the code. As in the previous tasks, we can divide the solution into three smaller tasks:
- Reading input from the console.
- Doing the calculations.
- Printing the output on the console.
We declare the variables that we are going to use and again we have to choose meaningful names, which are going to give us hints about the values they store. We initialize their values: with
Console.ReadLine(…), we read the input numbers from the console and convert the string entered by the user to
We do the necessary calculations:
Finally, we calculate the commission fee value and subtract it from the sum in Euro. Let's pay attention to the way we could write this:
Euro -= commission fee * Euro is the short way to write
Euro = Euro - (commission fee * Euro). In this case, we use a combined assignment operator (
-=) that subtracts the value of the operand to the right from the one to the left. The operator for multiplication (
*) has a higher priority than the combined operator, this is why, the expression
commission fee * Euro is performed first and then its value is divided.
The task does not specify special string formatting or rounding the result, therefore, we just have to calculate the output and print it on the console.
Let's pay attention to something that applies to all other problems of this type: written like that, the solution of the task is pretty detailed. As the task itself is not too complex, in theory, we could write one big expression, where right after having taken the input, we calculate the output. For example, such expression would look like this:
This code would print a correct result, but it is hard to read. It won't be easy to find out how it works and whether it contains any mistakes, as well as finding such and correcting them. Instead of one complex expression, it is better to write a few simpler ones and store their values in variables with appropriate names. This way, the code is more clean and easily maintainable.
Test your solution here: https://judge.softuni.org/Contests/Practice/Index/505#3.