Hướng dẫn finding sum of digits of a number until sum becomes single digit in c++ - tìm tổng các chữ số của một số cho đến khi tổng trở thành một chữ số trong C++

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

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 
4
1
3
1
4

If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
91
9
0 #include3

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
9
0
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

9
4

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
9
03

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
9
05

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
9
0 #include3

If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
2

Java

#include5

9
11

#include8

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

1
2
1
3 std;4namespace1namespace9

1
2
9
0 namespace1namespace2

1
2
9
0
9
33namespace8

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
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#include7 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
9
48

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

1
2int
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 
19
9
54namespace2

1
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 
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 
4
If 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
9
62

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
1
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 
46
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
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 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 
43

1
2
9
0 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 
4
1
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 
46
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 
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
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 namespace1__

1
2
9
0 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 
4
9
86
9
87

If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
91
9
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 
46
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 
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 
52
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
9
54

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

#include8 #include02

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

1
2
1
3 std;4namespace1namespace9

1
2
9
0 namespace1namespace2

1
2
9
0
9
33namespace8

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
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#include7 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
9
48

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

1
2int
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 
19
9
54namespace2

1
2
1
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 
4
If 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
9
62

1
36

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
1
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 
46
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
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 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 
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

1
2
9
0 namespace1

1
2
9
0 namespace1namespace2

1
2
9
0
9
33namespace8

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#include7 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
9
48

1
2int
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 
19
9
54namespace2

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
9
62

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
1
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 
46
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
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 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 
43

1
95

1
2
9
0 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 
4
1
3
1
4

1
2
9
0 namespace1namespace2

1
2
9
0
9
33namespace8

If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
2

9
03

#include82

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#include7 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
9
48
O[1]

1
2int
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 
19
9
54namespace2
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
9
62

//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 
4
1
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 
46
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
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 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 
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.
 


Bài Viết Liên Quan

Chủ Đề