Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sum of random numbers 1
- Scanner key = new Scanner(System.in);
- String digits = key.nextLine();// read digits as string
- int sum = 0;
- for (int i = 0; i < digits.length(); i++) {
- sum += Integer.parseInt(String.valueOf(digits.charAt(i))); //parse to int chars of digits
- }
- System.out.println(sum);
- // Sum of random numbers 2
- Scanner key = new Scanner (System.in);
- int num = Integer.parseInt(key.nextLine());
- int sum = 0;
- while (num > 0){
- sum += num % 10;
- num /= 10;
- }
- System.out.println(sum);*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement