Hướng dẫn how do you add digits to a single number in python? - làm cách nào để bạn thêm các chữ số vào một số trong python?

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc
    Examples: 
     

    Bàn luận
    Output : 15 
    Input : n = 111 
    Output : 3

    Cho một số và nhiệm vụ là tìm tổng số các chữ số của số này trong Python. & Nbsp; ví dụ: & nbsp; & nbsp;
    Below are the methods to sum of the digits. 
    Method-1: Using str() and int() methods.: The str() method is used to convert the number to string. The int() method is used to convert the string digit to an integer. 

    Đầu vào: n = 87 & nbsp; đầu ra: 15 & nbsp; đầu vào: n = 111 & nbsp; đầu ra: 3

    Python3

    & nbsp; bên dưới là các phương thức để tổng của các chữ số. & nbsp; Phương thức-1: sử dụng các phương thức str () và int () .: Phương thức str () được sử dụng để chuyển đổi số thành chuỗi. Phương thức int () được sử dụng để chuyển đổi chữ số chuỗi thành số nguyên. & Nbsp;

    Chuyển đổi số thành chuỗi và lặp qua từng chữ số trong chuỗi và sau khi chuyển đổi từng chữ số thành số nguyên và thêm vào tổng của các chữ số trong mỗi lần lặp. & Nbsp;

    def

    15
    0

    15
    1
    15
    2
    15
    3
    15
    4

    15
    1
    15
    6
    15
    7
    15
    8
    15
    9
    15
    0

    15
    1
    15
    2
    15
    3
    15
    3
    15
    5
    15
    6

    15
    3
    15
    4

    Output:

    15

    15
    1
    15
    8
    15
    2
    The sum() method is used to sum of numbers in the list.

    15
    0
    15
    3
    15
    2

    Python3

    & nbsp; bên dưới là các phương thức để tổng của các chữ số. & nbsp; Phương thức-1: sử dụng các phương thức str () và int () .: Phương thức str () được sử dụng để chuyển đổi số thành chuỗi. Phương thức int () được sử dụng để chuyển đổi chữ số chuỗi thành số nguyên. & Nbsp;

    Chuyển đổi số thành chuỗi và lặp qua từng chữ số trong chuỗi và sau khi chuyển đổi từng chữ số thành số nguyên và thêm vào tổng của các chữ số trong mỗi lần lặp. & Nbsp;

    def

    15
    0

    15
    1
    15
    2
    15
    3
    15
    4

    15
    1
    15
    2
    15
    3
    15
    3
    15
    5
    15
    6

    15
    3
    15
    4

    Output:

    15

    15
    1
    15
    8
    15
    2

    • 15
      0
      15
      3
      15
      2
    • Phương thức-2: Sử dụng phương thức SUM () .: Phương thức SUM () được sử dụng để tổng số trong danh sách.
    • Chuyển đổi số thành chuỗi bằng str () và dải chuỗi và chuyển đổi thành danh sách số bằng phương thức dải () và map () Phương thức resp. Sau đó tìm tổng bằng phương thức Sum ().
    • 15
      1
      15
      8
      15
      3
      15
      9
      If n < 10    
          digSum(n) = n
      Else         
          digSum(n) = Sum(digSum(n))
      1
    • 15
      1
      If n < 10    
          digSum(n) = n
      Else         
          digSum(n) = Sum(digSum(n))
      3
      15
      3
      If n < 10    
          digSum(n) = n
      Else         
          digSum(n) = Sum(digSum(n))
      5
      If n < 10    
          digSum(n) = n
      Else         
          digSum(n) = Sum(digSum(n))
      6
      If n < 10    
          digSum(n) = n
      Else         
          digSum(n) = Sum(digSum(n))
      7
      If n < 10    
          digSum(n) = n
      Else         
          digSum(n) = Sum(digSum(n))
      6
      15
      5
      Input : 1234
      Output : 1
      Explanation : The sum of 1+2+3+4 = 10, 
                    digSum(x) == 10
                    Hence ans will be 1+0 = 1
      
      Input : 5674
      Output : 4 
      0
    • 15
      1
      15
      8
      15
      2
      Input : 1234
      Output : 1
      Explanation : The sum of 1+2+3+4 = 10, 
                    digSum(x) == 10
                    Hence ans will be 1+0 = 1
      
      Input : 5674
      Output : 4 
      4

    Phương pháp-3: Sử dụng cách tiếp cận chung: & nbsp;

    Python3

    & nbsp; bên dưới là các phương thức để tổng của các chữ số. & nbsp; Phương thức-1: sử dụng các phương thức str () và int () .: Phương thức str () được sử dụng để chuyển đổi số thành chuỗi. Phương thức int () được sử dụng để chuyển đổi chữ số chuỗi thành số nguyên. & Nbsp;

    Chuyển đổi số thành chuỗi và lặp qua từng chữ số trong chuỗi và sau khi chuyển đổi từng chữ số thành số nguyên và thêm vào tổng của các chữ số trong mỗi lần lặp. & Nbsp;

    def

    15
    0

    15
    1
    15
    2
    15
    3
    15
    4

    15
    1
    15
    6
    15
    7
    15
    8
    15
    9
    15
    0

    15
    1
    15
    6
    15
    7
    15
    8
    15
    9
    15
    0

    15
    1
    15
    2
    15
    3
    15
    3
    15
    5
    15
    6

    15
    3
    15
    4

    Output:

    15

    15
    1
    15
    8
    15
    2

    Python3

    15
    0
    15
    3
    15
    2

    Phương thức-2: Sử dụng phương thức SUM () .: Phương thức SUM () được sử dụng để tổng số trong danh sách.

    15
    1
    15
    2
    15
    3
    15
    3
    15
    5
    15
    6

    15
    3
    15
    23

    Output:

    15

    Cho một số n, chúng ta cần tìm tổng số các chữ số của nó như vậy: & nbsp;n, we need to find the sum of its digits such that: 

    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))

    Ví dụ: & nbsp; 

    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 

    Cách tiếp cận vũ phu là tổng hợp tất cả các chữ số cho đến khi tổng <10. & nbsp; sơ đồ: & nbsp;brute force approach is to sum all the digits until the sum < 10. 
    Flowchart: 

    Hướng dẫn how do you add digits to a single number in python? - làm cách nào để bạn thêm các chữ số vào một số trong python?

    Dưới đây là chương trình vũ lực để tìm tổng. & NBSP;

    C++

    15
    24

    15
    25
    15
    26
    15
    27

    15
    5
    15
    29
    15
    5
    15
    31

    15
    32

    15
    1
    15
    5
    15
    35

    15
    1
    1
    7
    15
    38

    15
    1
    15
    32

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    01
    15
    43

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    32

    15
    46
    15
    47

    15
    46
    15
    35

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    51

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    53

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    55

    15
    1
    15
    51

    15
    1
    15
    8
    15
    60

    15
    51

    15
    5
    15
    63

    15
    32

    15
    1
    15
    5
    15
    67

    15
    1
    15
    69

    15
    1
    15
    8
    15
    72

    15
    51

    Java

    15
    74
    15
    75

    15
    76
    15
    77
    15
    78

    15
    1
    15
    80
    15
    5
    15
    29
    15
    5
    15
    31

    15
    1
    15
    32

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    5
    15
    89
    15
    4
    15
    91

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    1
    7
    15
    94
    15
    4
    15
    96
    15
    97
    9
    0

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    32

    15
    46
    15
    01
    15
    03
    15
    4
    15
    05

    15
    06
    15
    47

    15
    06
    15
    89
    15
    4
    15
    91

    15
    46
    15
    51

    15
    46
    15
    15
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    9
    15
    91

    15
    46
    15
    19
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    9
    15
    91

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    51

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    15
    60

    15
    1
    15
    51

    15
    1
    15
    76
    15
    80
    15
    32
    15
    33

    15
    1
    15
    32

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    5
    15
    38
    15
    39
    15
    91

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    42

    15
    1
    15
    51

    15
    51

    Python

    15
    74
    15
    47

    def

    15
    49

    15
    1
    15
    2
    15
    3
    15
    4

    15
    1
    1
    7
    15
    94
    15
    4
    15
    58
    15
    2
    15
    60__

    Các

    15
    46
    15
    0
    15
    3
    15
    2

    15
    46
    15
    2
    15
    3
    15
    4

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    2
    15
    3
    15
    3
    15
    0
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    8
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    9

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    0
    9
    55____13
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    9

    15
    1
    15
    8
    15
    2

    15
    0
    15
    3
    15
    39

    15
    3
    15
    97

    C#

    15
    25
    15
    99

    15
    77
    15
    01

    15
    1
    15
    80
    15
    5
    15
    29
    15
    5
    15
    31

    15
    1
    15
    32

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    5
    15
    89
    15
    4
    15
    91

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    1
    7
    15
    94
    15
    4
    15
    96
    15
    97
    9
    0

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    32

    15
    46
    15
    01
    15
    03
    15
    4
    15
    05

    15
    46
    15
    32

    15
    06
    15
    47

    15
    06
    15
    35

    15
    46
    15
    51

    15
    46
    15
    53

    15
    46
    15
    55

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    51

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    15
    60

    15
    1
    15
    51

    15
    1
    15
    76
    15
    80
    15
    32
    15
    33

    15
    1
    15
    32

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    5
    15
    38
    15
    39
    15
    91

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    51

    15
    1
    15
    51

    15
    51

    Python

    15
    55

    15
    74
    15
    47

    15
    32

    def

    15
    49

    15
    1
    15
    2
    15
    3
    15
    4

    15
    1
    15
    32

    15
    1
    1
    7
    15
    94
    15
    4
    15
    58
    15
    2
    15
    60__

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    32

    Các

    15
    46
    15
    0
    15
    3
    15
    2

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    51

    15
    46
    15
    2
    15
    3
    15
    4

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    2
    15
    3
    15
    3
    15
    0
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    8
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    9

    15
    1
    15
    51

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    0
    9
    55____13
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    9

    15
    51

    15
    1
    15
    8
    15
    2

    15
    0
    15
    3
    15
    39

    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    13

    15
    3
    15
    97

    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    14

    15
    1
    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    16

    15
    25
    15
    99

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    21

    15
    77
    15
    01

    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    25
    15
    01
    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    27

    15
    06
    15
    47

    15
    06
    15
    35

    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    25
    15
    51

    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    25
    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    35

    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    25
    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    37

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    51

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    15
    60

    15
    1
    15
    51

    15
    1
    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    46

    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    47

    1511576 1580 1532 1533

    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    48

    15
    5
    15
    29
    15
    5
    15
    31

    15
    32

    15
    1
    15
    5
    15
    35

    15
    1
    1
    7
    15
    38

    15
    1
    15
    32

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    01
    15
    43

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    32

    15
    46
    15
    47

    15
    46
    15
    35

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    5
    15
    38
    15
    39
    15
    91

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    53

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    55

    15
    1
    15
    51

    15
    1
    15
    8
    15
    60

    15
    51

    15
    5
    15
    63

    15
    32

    15
    1
    15
    5
    15
    67

    15
    1
    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    90
    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    6
    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    92
    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    93

    15
    1
    15
    8
    15
    72

    15
    51

    Java 

    1

    15
    74
    15
    75
    O(log(n)).
    Auxiliary Space: O(1)

    15
    76
    15
    77
    15
    78

    15
    1
    15
    80
    15
    5
    15
    29
    15
    5
    15
    31
    There exists a simple and elegant O(1) solution for this too. The answer is given simply:- 

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 

    Làm thế nào để logic trên hoạt động? & NBSP;

    Logic đằng sau cách tiếp cận này là:

    Để kiểm tra xem một số có chia hết cho 9 không, hãy thêm các chữ số của số và kiểm tra xem tổng có chia hết cho 9 hay không. Nếu có, là trường hợp, & nbsp; con số chia hết cho 9, nếu không, nó không.

    Chúng ta hãy lấy 27 & nbsp; i.e (2+7 = 9) do đó chia hết cho 9. Nếu một số n chia hết cho 9, thì tổng của nó cho đến khi tổng trở thành một chữ số luôn luôn là 9. Ví dụ: & nbsp; , n = 2880 & nbsp; tổng các chữ số = 2 + 8 + 8 = 18: 18 = 1 + 8 = 9
    If a number n is divisible by 9, then the sum of its digit until the sum becomes a single digit is always 9. For example, 
    Let, n = 2880 
    Sum of digits = 2 + 8 + 8 = 18: 18 = 1 + 8 = 9

    Do đó, một số có thể có dạng 9x hoặc 9x + k. Đối với trường hợp đầu tiên, câu trả lời luôn là 9. Đối với trường hợp thứ hai và luôn luôn là K đó là phần còn lại còn lại.
    A number can be of the form 9x or 9x + k. For the first case, the answer is always 9. For the second case, and is always k which is the remainder left.

    Vấn đề được gọi là vấn đề gốc chữ số.

    Bạn có thể thấy bài viết Wikipedia này hữu ích. -> https://en.wikipedia.org/wiki/digital_root

    Dưới đây là việc thực hiện ý tưởng trên: & NBSP;

    C++

    15
    24

    15
    25
    15
    26
    15
    27

    15
    5
    15
    29
    15
    5
    15
    31

    15
    32

    15
    1
    15
    01
    15
    43

    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    10
    15
    8
    15
    72

    15
    1
    15
    8
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    15

    15
    51

    15
    5
    15
    63

    15
    32

    15
    1
    15
    5
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    22

    15
    1
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    24

    15
    1
    15
    8
    15
    72

    15
    51

    Java

    15
    74
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    30

    15
    77
    15
    01

    15
    1
    15
    80
    15
    5
    15
    29
    15
    5
    15
    31

    15
    1
    15
    32

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    01
    15
    03
    15
    4
    9
    0

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    15
    4
    15
    91

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    522
    15
    97

    15
    1
    15
    51

    15
    1
    15
    76
    15
    80
    15
    32
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    67

    15
    1
    15
    32

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    5
    15
    38
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    73
    15
    91

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    42

    15
    1
    15
    51

    15
    51

    Python3

    def

    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    81

    15
    1
    15
    01
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    7
    15
    3
    15
    3
    15
    4
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    1

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    15
    4

    15
    1
    15
    01
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    7 ____78
    15
    97

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    15
    97

    15
    1
    15
    06
    1
    06

    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    10
    15
    8
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    7 ____78
    15
    97
    9
    0

    15
    0
    15
    3
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    73

    15
    3
    15
    97

    C#

    15
    25
    15
    99

    15
    77
    1
    21

    15
    32

    15
    1
    15
    80
    15
    5
    15
    29
    15
    5
    15
    31

    15
    1
    15
    32

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    01
    15
    03
    15
    4
    9
    0

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    15
    4
    15
    91

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    522
    15
    97

    15
    1
    15
    51

    15
    1
    15
    76
    15
    80
    15
    32
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    67

    15
    1
    15
    32

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    5
    15
    38
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    73
    15
    91

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    51

    15
    1
    15
    51

    15
    51

    def Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum(x) == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 4 81

    15
    55

    15
    1
    15
    01
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    7
    15
    3
    15
    3
    15
    4
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    1

    15
    32

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    15
    4

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    15
    4
    15
    91

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    522
    15
    97

    15
    51

    15
    1
    15
    76
    15
    80
    15
    32
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    67

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    5
    15
    38
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    73
    15
    91

    def

    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    81

    1511501 If n == 0 return 0; If n % 9 == 0 digSum(n) = 9 Else digSum(n) = n % 9 7153153 154If n == 0 return 0; If n % 9 == 0 digSum(n) = 9 Else digSum(n) = n % 9 1

    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    14

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    15
    4

    15
    32

    15
    1
    15
    01
    15
    43

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    15
    4
    15
    91

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    8
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    522
    15
    97

    15
    51

    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    22

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    01

    If n < 10    
        digSum(n) = n
    Else         
        digSum(n) = Sum(digSum(n))
    47

    Output:  

    9

    15
    1
    15
    76
    15
    80
    15
    32
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    67
    O(1)

    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    2
    15
    5
    15
    38
    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    73
    15
    91
    O(1)

    def

    Input : 1234
    Output : 1
    Explanation : The sum of 1+2+3+4 = 10, 
                  digSum(x) == 10
                  Hence ans will be 1+0 = 1
    
    Input : 5674
    Output : 4 
    81
    https://www.geeksforgeeks.org/digital-rootrepeated-digital-sum-given-integer/

    15
    1
    15
    01
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    7
    15
    3
    15
    3
    15
    4
    If n == 0
       return 0;
    
    If n % 9 == 0      
        digSum(n) = 9
    Else               
        digSum(n) = n % 9 
    1Ayush Khanduri. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.