Hướng dẫn sum of digits until single digit in python - tổng các chữ số cho đến một chữ số trong python

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

#include<bits/stdc++.h>

using namespace std;

int 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__ 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 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 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 413 14

If n == 0 return 0; If n % 9 == 0 digSum(n) = 9 Else digSum(n) = n % 9 9190 #include<bits/stdc++.h>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 490 If n == 0 return 0; If n % 9 == 0 digSum(n) = 9 Else digSum(n) = n % 9 96

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

int 94

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 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 4int 903

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 4905

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 490 #include<bits/stdc++.h>3

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

Java

#include<bits/stdc++.h>5 911

#include<bits/stdc++.h>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 82

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 4using1 int 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 0int 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 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 4Input : 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 3

1213 std;4namespace1namespace9

1290 namespace1namespace2

1290 933namespace8

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 4If n == 0 return 0; If n % 9 == 0 digSum(n) = 9 Else digSum(n) = n % 9 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#include<bits/stdc++.h>7 using1 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 13 948

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 4Input : 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 3

12int 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 19954namespace2

12Input : 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 23

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 4If n == 0 return 0; If n % 9 == 0 digSum(n) = 9 Else digSum(n) = n % 9 2

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

Python3

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 29 962

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 413 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 46Input : 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 33Input : 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 33 namespace1Input : 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 43

1290 namespace1

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 413 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 46Input : 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 64 namespace8 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 33__

1290 namespace8

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 4986987

If n == 0 return 0; If n % 9 == 0 digSum(n) = 9 Else digSum(n) = n % 9 9190 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 46Input : 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 64 namespace8namespace9

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 52Input : 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 33 954

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 77Input : 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 78

C#

using 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 80

#include<bits/stdc++.h>8 #include<bits/stdc++.h>02

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 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 4using1 int 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 0int 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 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 4Input : 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 3

1213 std;4namespace1namespace9

1290 namespace1namespace2

1290 933namespace8

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 4If n == 0 return 0; If n % 9 == 0 digSum(n) = 9 Else digSum(n) = n % 9 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#include<bits/stdc++.h>7 using1 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 13 948

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 4Input : 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 3

12int 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 19954namespace2

12132

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 4If n == 0 return 0; If n % 9 == 0 digSum(n) = 9 Else digSum(n) = n % 9 2

If n == 0 return 0; If n % 9 == 0 digSum(n) = 9 Else digSum(n) = n % 9 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 29 962

136

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 413 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 46Input : 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 33Input : 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 33 namespace1Input : 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 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 3

1290 namespace1

1290 namespace1namespace2

1290 933namespace8

If n == 0 return 0; If n % 9 == 0 digSum(n) = 9 Else digSum(n) = n % 9 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#include<bits/stdc++.h>7 using1 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 13 948

12int 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 19954namespace2

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 29 962

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 413 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 46Input : 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 33Input : 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 33 namespace1Input : 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 43

195

1290 namespace1

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 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 413 14

1290 namespace1namespace2

1290 933namespace8

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

903

#include<bits/stdc++.h>82

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

Output:  

9

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#include<bits/stdc++.h>7 using1 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 13 948 O(1)

12int 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 19954namespace2 O(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 29 962
//www.geeksforgeeks.org/digital-rootrepeated-digital-sum-given-integer/

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 413 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 46Input : 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 33Input : 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 33 namespace1Input : 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 43Ayush 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.
 


Làm thế nào để bạn thêm các chữ số vào một số duy nhất trong Python?

Các phương pháp khác nhau để tìm tổng các chữ số của một số trong Python. Để chuyển đổi một số thành một chuỗi, hãy sử dụng hàm str (). Để chuyển đổi một chữ số chuỗi thành một số nguyên, hãy sử dụng hàm int (). Chuyển đổi số thành một chuỗi, lặp qua mỗi chữ số trong chuỗi và thêm vào tổng của các chữ số trong mỗi lần lặp.To convert a number to a string, use the str() function. To convert a string digit to an integer, use the int() function. Convert the number to a string, iterate over each digit in the string, and add to the sum of the digits in each iteration.

Làm thế nào để bạn tạo một vòng lặp lặp lại cho đến khi tổng là một chữ số?

Nhắc: Viết một chương trình thêm tất cả các chữ số trong một số nguyên. Nếu tổng kết quả là nhiều hơn một chữ số, hãy tiếp tục lặp lại cho đến khi tổng là một chữ số.Ví dụ: số 2345 có tổng 2+3+4+5 = 14 không phải là một chữ số nên lặp lại với 1+4 = 5 là một chữ số.Write a program that adds all the digits in an integer. If the resulting sum is more than one digit, keep repeating until the sum is one digit. For example, the number 2345 has the sum 2+3+4+5 = 14 which is not a single digit so repeat with 1+4 = 5 which is a single digit.

Làm thế nào để bạn viết tổng các chữ số trong Python?

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 () resp. Sau đó tìm tổng bằng phương thức sum ().Convert the number to string using str() and strip the string and convert to list of number using strip() and map() method resp. Then find the sum using the sum() method.

Làm thế nào để bạn giảm xuống một chữ số?

Đưa ra một số N, nhiệm vụ là giảm nó thành một số một chữ số bằng cách liên tục trừ các chữ số liền kề.Đó là, trong lần lặp đầu tiên, trừ tất cả các chữ số liền kề để tạo một số mới, nếu số này chứa nhiều hơn một chữ số, hãy lặp lại cùng một quy trình cho đến khi nó trở thành một số một chữ số.repeatedly subtracting the adjacent digits. That is, in the first iteration, subtract all the adjacent digits to generate a new number, if this number contains more than one digit, repeat the same process until it becomes a single-digit number.

Chủ đề