Advertisement
CR7CR7

LeftRight

May 23rd, 2023
1,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ADEX2_FromLeftToTheRight {
  4.  public static void main(String[] args) {
  5.   Scanner scanner = new Scanner(System.in);
  6.   // get the number of lines as input
  7.   int n = Integer.parseInt(scanner.nextLine());
  8.   // loop through each line
  9.   for (int i = 0; i < n; i++) {
  10.    // get the two numbers as strings
  11.    String[] numbers = scanner.nextLine().split(" ");
  12.    // convert them to long values
  13.    long firstNum = Long.parseLong(numbers[0]);
  14.    long secondNum = Long.parseLong(numbers[1]);
  15.    // initialize the sum of digits to zero
  16.    int sumOfDigits = 0;
  17.    // compare the two numbers
  18.    if (firstNum > secondNum) {
  19.     // if the left number is greater, get the absolute value of it
  20.     firstNum = Math.abs(firstNum);
  21.     // loop through each digit of the left number
  22.     while (firstNum > 0) {
  23.      // add the last digit to the sum of digits
  24.      sumOfDigits += firstNum % 10;
  25.      // remove the last digit from the left number
  26.      firstNum /= 10;
  27.     }
  28.    } else {
  29.     // if the right number is greater or equal, get the absolute value of it
  30.     secondNum = Math.abs(secondNum);
  31.     // loop through each digit of the right number
  32.     while (secondNum > 0) {
  33.      // add the last digit to the sum of digits
  34.      sumOfDigits += secondNum % 10;
  35.      // remove the last digit from the right number
  36.      secondNum /= 10;
  37.     }
  38.    }
  39.    // print the sum of digits
  40.    System.out.println(sumOfDigits);
  41.   }
  42.  }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement