Cải thiện bài viết
Lưu bài viết
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
150
151
152
153
154
151
156
157
158
159
150
151
152
153
153
155
156
153
154
Output:
15
15
115
8 15
2The sum[] method is used to sum of numbers in the list.
150
153
152
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
150
151
152
153
154
151
152
153
153
155
156
153
154
Output:
15
15
115
8 15
2
15
015
315
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
115
815
315
9If n < 10 digSum[n] = n Else digSum[n] = Sum[digSum[n]]
115
1If n < 10 digSum[n] = n Else digSum[n] = Sum[digSum[n]]
315
3If n < 10 digSum[n] = n Else digSum[n] = Sum[digSum[n]]
5If n < 10 digSum[n] = n Else digSum[n] = Sum[digSum[n]]
6If n < 10 digSum[n] = n Else digSum[n] = Sum[digSum[n]]
7If n < 10 digSum[n] = n Else digSum[n] = Sum[digSum[n]]
615
5Input : 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
015
115
815
2Input : 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
150
151
152
153
154
151
156
157
158
159
150
151
156
157
158
159
150
151
152
153
153
155
156
153
154
Output:
15
15
115
8 15
2
Python3
150
153
152
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.
151
152
153
153
155
156
153
1523
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++
1524
1525
1526
1527
155
1529
155
1531
1532
151
1501
1543
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 : 410
158
1572
151
158
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 : 415
1551
155
1563
1532
151
155
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 : 422
151
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 : 424
151
158
1572
1551
Java
1574
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 : 430
1577
1501
151
1580
155
1529
155
1531
151
1532
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
1501
1503
154
90
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
158
154
1591
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
158
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 : 4522
1597
151
1551
151
1576
1580
1532
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 : 467
151
1532
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
155
1538
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 : 473
1591
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
1542
151
1551
1551
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 : 481
151
1501
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 97
153
153
154
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 91
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
158
154
151
1501
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 97 ____78
1597
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
158
1597
151
1506
106
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 : 410
158
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 97 ____78
1597
90
150
153
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 : 473
153
1597
C#
1525
1599
1577
121
1532
151
1580
155
1529
155
1531
151
1532
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
1501
1503
154
90
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
158
154
1591
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
158
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 : 4522
1597
151
1551
151
1576
1580
1532
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 : 467
151
1532
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
155
1538
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 : 473
1591
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
1551
151
1551
1551
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
1555
151
1501
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 97
153
153
154
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 91
1532
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
158
154
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
158
154
1591
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
158
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 : 4522
1597
1551
151
1576
1580
1532
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 : 467
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
155
1538
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 : 473
1591
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 : 481
15
115
01 If n == 0
return 0;
If n % 9 == 0
digSum[n] = 9
Else
digSum[n] = n % 9
715
315
3 15
4If 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 % 92
158
154
1532
151
1501
1543
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
158
154
1591
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 92
158
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 : 4522
1597
1551
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 : 422
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 901
If n < 10 digSum[n] = n Else digSum[n] = Sum[digSum[n]]47
Output:
9
15
115
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
215
5 15
38Input : 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
7315
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/
151
1501
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 97
153
153
154
If n == 0 return 0; If n % 9 == 0 digSum[n] = 9 Else digSum[n] = n % 91Ayush 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.