Advertisement
Guest User

Untitled

a guest
Feb 20th, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | Software | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Energy {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int n = scanner.nextInt();
  8.         int digit = 0;
  9.         int sumOdd = 0;
  10.         int sumEven = 0;
  11.         int length = String.valueOf(n).length();
  12.  
  13.         //calculate the sum of the even and odd numbers separately
  14.  
  15.         while (n > 0 && length <= 18) {
  16.             digit = n % 10;
  17.             if (digit % 2 == 0 || digit == 0) {
  18.                 sumEven += digit;
  19.             } else if (digit % 2 == 1 || digit == 1) {
  20.                 sumOdd += digit;
  21.             }
  22.             n -= digit;
  23.             n /= 10;
  24.  
  25.         }
  26.  
  27.           /*
  28.          
  29.           if (sumOdd > sumEven) {
  30.                 System.out.printf("%.0f cups of coffee", sumOdd);
  31.             } else if (sumEven > sumOdd) {
  32.                 System.out.printf("%.0f energy drinks", sumEven);
  33.             } else {
  34.                 System.out.printf("%.0f of both", sumEven);
  35.             }
  36.            
  37.             */
  38.  
  39.         if (sumOdd > sumEven) {
  40.             System.out.printf(sumOdd + " cups of coffee");
  41.         } else if (sumEven > sumOdd) {
  42.             System.out.printf(sumEven + " energy drinks");
  43.         } else {
  44.             System.out.printf(sumEven + " of both");
  45.         }
  46.  
  47.         }
  48.     }
Tags: Java
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement