Advertisement
myrdok123

05. Coins

Feb 4th, 2024
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. package W05WhileLoop.Exercises;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P05Coins {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         double sum = Double.parseDouble(scanner.nextLine());
  12.  
  13.         //Превръщаме всичко в стотинки
  14.         double totalCoins = Math.floor(sum * 100);
  15.  
  16.         int countCoins = 0;
  17.  
  18.         //Правим while -> докато totalCoins > 0
  19.         while (totalCoins > 0){
  20.  
  21.             //Проверяваме коя е най-голямата възможна монета, с която можем да върнем
  22.             if(totalCoins >= 200){
  23.                 countCoins ++;
  24.                 totalCoins -= 200;
  25.             } else if (totalCoins >= 100) {
  26.                 countCoins++;
  27.                 totalCoins -= 100;
  28.             } else if (totalCoins >= 50) {
  29.                 countCoins++;
  30.                 totalCoins -= 50;
  31.             } else if (totalCoins >= 20) {
  32.                 countCoins++;
  33.                 totalCoins -= 20;
  34.             } else if (totalCoins >= 10) {
  35.                 countCoins++;
  36.                 totalCoins -= 10;
  37.             }else if (totalCoins >= 5) {
  38.                 countCoins++;
  39.                 totalCoins -= 5;
  40.             }else if (totalCoins >= 2) {
  41.                 countCoins++;
  42.                 totalCoins -= 2;
  43.             }else if (totalCoins >= 1) {
  44.                 countCoins++;
  45.                 totalCoins -= 1;
  46.             }
  47.         }
  48.         System.out.println(countCoins);
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement