Didart

Sum Digits

Sep 24th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.45 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SumDigits {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int number = Integer.parseInt(scanner.nextLine());
  8.        
  9.         int sum = 0;
  10.         while (number != 0) {
  11.             int lastDigit = number % 10;
  12.             sum += lastDigit;
  13.             number = number / 10;   //number /= 10;
  14.         }
  15.         System.out.println(sum);
  16.     }
  17. }
  18.  
  19.  
Advertisement
Add Comment
Please, Sign In to add comment