desislava_topuzakova

02. Sum Digits

May 29th, 2021
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Division_02 {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int number = Integer.parseInt(scanner.nextLine());
  7.         //спирам да сумирам, когато нямам цифри (число = 0)
  8.         //продължавам => число != 0
  9.             //взимам последната цифра
  10.             //добавям я към сумата
  11.             //премахвам последната цифра
  12.  
  13.         int sum = 0; //сума на цифрите
  14.         while (number != 0) {
  15.             int lastDigit = number % 10; //последната цифра
  16.             sum += lastDigit;
  17.             number = number / 10; // number /= 10
  18.         }
  19.  
  20.         System.out.println(sum);
  21.  
  22.  
  23.     }
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment