Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int number = Integer.parseInt(scanner.nextLine()),
- sumDigits = 0;
- while (number > 0) {
- sumDigits += number % 10;
- number /= 10;
- }
- System.out.println(sumDigits);
- }
- }
- OR:
- import java.util.Scanner;
- class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String number = scanner.nextLine();
- int sumDigits = 0;
- for (int i = 0; i < number.length(); i++) {
- sumDigits += Character.getNumericValue(number.charAt(i));
- }
- System.out.println(sumDigits);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement