Statistics

Problem Statement for "DigitsSum"

Problem Statement

Little Johnny is in the first grade and just found out about addition. He quickly learned the addition table and is becoming bored in class, so he thinks of a game. The game consists of choosing an integer and summing its digits. If the result is a single digit, the game ends. Otherwise, Johnny repeats the process on the result until he gets a single digit.

For example, if Johnny starts with the number 12345, he will add the digits 1 + 2 + 3 + 4 + 5 to get 15. The result is not a single digit, so he will repeat the process to get 1 + 5 = 6. The new result is a single digit, so the game ends.

Johnny is curious to see if his computations are correct, so he asks you what the final result is for the number n.

Definition

Class:
DigitsSum
Method:
lastDigit
Parameters:
int
Returns:
int
Method signature:
int lastDigit(int n)
(be sure your method is public)

Constraints

  • n will be between 0 and 2147483647, inclusive.

Examples

  1. 12345

    Returns: 6

    The example in the problem statement.

  2. 6

    Returns: 6

    In this simple example, the sum of the digits is 6, so the game ends after just one step.

  3. 999999999

    Returns: 9

    The sum of the digits is 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 = 81. The process is repeated once more to get 8 + 1 = 9, which is a single digit.

  4. 213413512

    Returns: 4

  5. 314

    Returns: 8

  6. 0

    Returns: 0

  7. 1

    Returns: 1

  8. 9

    Returns: 9

  9. 1234567890

    Returns: 9

  10. 34222432

    Returns: 4

  11. 1999999999

    Returns: 1

  12. 888888787

    Returns: 7

  13. 2099999999

    Returns: 2

  14. 2

    Returns: 2

  15. 3

    Returns: 3

  16. 4

    Returns: 4

  17. 5

    Returns: 5

  18. 6

    Returns: 6

  19. 7

    Returns: 7

  20. 8

    Returns: 8

  21. 9

    Returns: 9

  22. 10

    Returns: 1

  23. 11

    Returns: 2

  24. 2147483646

    Returns: 9

  25. 2147483645

    Returns: 8

  26. 2147483644

    Returns: 7

  27. 2147483643

    Returns: 6

  28. 2147483642

    Returns: 5

  29. 2147483641

    Returns: 4

  30. 2147483640

    Returns: 3

  31. 2147483639

    Returns: 2

  32. 2147483638

    Returns: 1

  33. 1527226606

    Returns: 1

  34. 617532711

    Returns: 6

  35. 1524386967

    Returns: 6

  36. 1392101304

    Returns: 6

  37. 1445573421

    Returns: 9

  38. 36281042

    Returns: 8

  39. 932706973

    Returns: 1

  40. 1864458436

    Returns: 4

  41. 889257998

    Returns: 2

  42. 291712976

    Returns: 8

  43. 94167379

    Returns: 1

  44. 309113759

    Returns: 2

  45. 509258979

    Returns: 9

  46. 856596139

    Returns: 7

  47. 96881055

    Returns: 6

  48. 493249945

    Returns: 4

  49. 1954042703

    Returns: 8

  50. 519801233

    Returns: 5

  51. 43666767

    Returns: 9

  52. 2000195275

    Returns: 4

  53. 613438109

    Returns: 8

  54. 300392873

    Returns: 8

  55. 953727234

    Returns: 6

  56. 557000384

    Returns: 5

  57. 1576770748

    Returns: 7

  58. 882870662

    Returns: 2

  59. 1761993397

    Returns: 1

  60. 1591826736

    Returns: 3

  61. 742987995

    Returns: 6

  62. 315501753

    Returns: 3

  63. 217587438

    Returns: 9

  64. 155198696

    Returns: 5

  65. 1821791645

    Returns: 8

  66. 1532542592

    Returns: 2

  67. 233774719

    Returns: 7

  68. 925681349

    Returns: 2

  69. 9343

    Returns: 1

  70. 4470

    Returns: 6

  71. 9420

    Returns: 6

  72. 528

    Returns: 6

  73. 7992

    Returns: 9

  74. 4670

    Returns: 8

  75. 253

    Returns: 1

  76. 4900

    Returns: 4

  77. 6932

    Returns: 2

  78. 2150

    Returns: 8

  79. 1000000000

    Returns: 1

  80. 2147483647

    Returns: 1

  81. 9

    Returns: 9

  82. 0

    Returns: 0

  83. 991

    Returns: 1

  84. 8888888

    Returns: 2

  85. 2147483647

    Returns: 1

  86. 12345

    Returns: 6

  87. 10

    Returns: 1

  88. 999999999

    Returns: 9

  89. 1

    Returns: 1

  90. 55

    Returns: 1

  91. 99

    Returns: 9

  92. 2999

    Returns: 2


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: