myrdok123

P05Coins

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