Advertisement
Kancho

Sum_Of_Random_Digits

Feb 26th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. // Sum of random numbers 1
  2.  
  3. Scanner key = new Scanner(System.in);
  4.  
  5. String digits = key.nextLine();// read digits as string
  6. int sum = 0;
  7. for (int i = 0; i < digits.length(); i++) {
  8. sum += Integer.parseInt(String.valueOf(digits.charAt(i))); //parse to int chars of digits
  9. }
  10. System.out.println(sum);
  11.  
  12. // Sum of random numbers 2
  13.  
  14. Scanner key = new Scanner (System.in);
  15. int num = Integer.parseInt(key.nextLine());
  16. int sum = 0;
  17. while (num > 0){
  18. sum += num % 10;
  19. num /= 10;
  20.  
  21. }
  22. System.out.println(sum);*/
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement