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 //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

    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 < 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
    //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.
     


    Bài Viết Liên Quan

    Chủ Đề