Hướng dẫn how do you find the single digit of a number in python? - làm thế nào để bạn tìm thấy một chữ số của một số trong python?

& nbsp; Harry & nbsp; ngày 26 tháng 8 năm 2022Harry  August 26, 2022

Tác giả: HarryHarry

Xin chào các bạn, cảm ơn vì đã ghé thăm trang web của tôi. Tôi là một lập trình viên Python. Tôi, với một số thành viên khác, viết blog trên trang web này dựa trên Python và lập trình. Chúng tôi vẫn đang trong giai đoạn phát triển đó là lý do tại sao thiết kế trang web không tốt và có nhiều thứ khác cần được sửa chữa trong trang web này nhưng tôi hy vọng tất cả những điều này sẽ xảy ra vào một ngày nào đó. Nhưng, cho đến lúc đó chúng ta sẽ không ngăn mình tải lên các bài viết tuyệt vời hơn. Nếu bạn muốn tham gia với chúng tôi hoặc có bất kỳ câu hỏi nào, bạn có thể gửi thư cho tôi cảm ơn bạn

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
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 
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 
40
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 
59

str_years = [x for x in range[24]]
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]

#Or, if you're starting with ints:
int_years = [int[x] for x in str_years]

#Formatted here
form_years = ["%02d" % x for x in int_years]

print[form_years]
#['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '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 
3
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1 using2n, we need to find the sum of its digits such that: 

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
4
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
47
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
65 using9
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34__
 

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 

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
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1 using9brute force approach is to sum all the digits until the sum < 10. 
Flowchart: 

1
92
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]]
47
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
65 using9namespace0

C++

#include

using namespace std;

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
1
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
4

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
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 
5

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

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]]
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 
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 
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 
3
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
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 
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 
8
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
7

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
3

1
92
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1
9
4

1
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n == 0
   return 0;

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
4

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]]
0
If n == 0
   return 0;

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1
9
4

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1
9
4

1
3

Java

9
6
If n == 0
   return 0;

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

9
9
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
83

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5#include2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
1
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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]]
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 
4 namespace5using2namespace0

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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
9 using6using2 using8using9namespace0

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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
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 
4 namespace5using2namespace7

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 
9

namespace8using1using2using3

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 
8
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 
8std;7std;8using3

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 
8
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
01std;8using3

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
9
8 #include2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
14
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
15

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]]
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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
20
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
21using3

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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
24

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
3

1
3

Python

9
6
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
29

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
30
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
31

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]]
33
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34 using2

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]]
9using6____72
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
40
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
47
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34 using2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
44

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 
8
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
53
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
8
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
33
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34 using2

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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
33
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
62
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
53
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
65 std;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 
3
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
53
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
69
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34 std;8

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
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]]
33

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
53
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
21

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
78
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
79

C#

using

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
81

9
9
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
83

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5#include2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
1
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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]]
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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
7

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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
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 
3
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
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 
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 
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 
8
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
4

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 
9

namespace8

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
7

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

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
9
8 #include2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
14
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
15

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]]
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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
20
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
21using3

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 
33

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
3

1
3

Python

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 
37

9
6
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
29

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
4

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
30
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
31

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]]
33
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34 using2

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

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]]
9using6____72
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
40
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
3
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
4
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
47
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34 using2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
44

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 
8
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
53
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
3
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 
8
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
33
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34 using2

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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
33
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
62
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
53
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
65 std;8

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
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 
3
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
53
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
69
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34 std;8

1
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
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]]
33

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
53
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
21

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 
95

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
78
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
79

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 
96

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
98

using

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
81

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
03

9
9
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
83

1
07
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
09

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 
9

namespace8

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
7

1
07
1
3

1
07
1
17

1
07
1
19

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
28

1
29

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5#include2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
1
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
3

1
30

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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
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]]
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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
0

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

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]]
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 
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 
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 
3
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
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 
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 
8
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
7

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
9
8 #include2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
14
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 
26

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 
3
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n == 0
   return 0;

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
4

PHP

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
72
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 
48
1
74
1
75

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 
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 
39
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 
40namespace0

1
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
44
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 
45
 

1

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]]
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 
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 
40
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 
50
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 
44
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
O[log[n]].
Auxiliary Space: 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 
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
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 
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 
40
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 
59

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

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

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 
44
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 
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 
40
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 
76

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
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 
44using3
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

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 
40
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 
90
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.

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 
91
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
40
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 
94

JavaScript

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
38
1
01

C++

#include

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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
9
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 
3
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
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]]
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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
0

1
92
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1
9
4

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1
1
97

1
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n == 0
   return 0;

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
4

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]]
0
If n == 0
   return 0;

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
If n == 0
   return 0;

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1
9
4

1
3

Java

9
6
If n == 0
   return 0;

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

9
9
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
83

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5#include2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
1
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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]]
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 
4 namespace5using2namespace0

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
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1 using2using3

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
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 
34using9
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
36__72

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
9
8 #include2
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 
49

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]]
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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
20
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
555____73

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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
24

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
3

1
3

Python3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
30
If n == 0
   return 0;

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
4
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
47
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34 using2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
44

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
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1 using2

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
4
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
47
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
65 using9
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34__

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
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1 using9

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
87
If n == 0
   return 0;

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

1
92
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]]
47
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
65 using9namespace0

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
53
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n == 0
   return 0;

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
78
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
79

C#

using

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
81

9
9
9
03

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
4

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5#include2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
1
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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]]
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 
4 namespace5using2namespace0

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
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1 using2using3

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
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 
34using9
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
36__72

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
9
8 #include2
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 
49

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]]
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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
20
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
555____73

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 
33

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
1
3

1
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
30
If n == 0
   return 0;

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

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 
37

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
4
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
47
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34 using2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
44

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1 using2

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
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1 using2using3

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
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 
34using9
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
36__72

1
3

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
9
8 #include2
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 
49

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
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
20
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
555____73

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
30
If n == 0
   return 0;

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

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
4
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
47
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34 using2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
44

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 
96

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
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1 using2

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
4

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
4
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
47
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
65 using9
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34__

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
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
1 using2using3

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
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 
34using9
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
36__72

1
3

If n == 0
   return 0;

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

9
83

1
29

Output:  

9

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
5
9
8 #include2
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 
49
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 
3
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
0
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
20
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
555____73
O[1]

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
30
If n == 0
   return 0;

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

//www.geeksforgeeks.org/digital-rootrepeated-digital-sum-given-integer/

If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
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 
4
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
47
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
34 using2
If n < 10    
    digSum[n] = n
Else         
    digSum[n] = Sum[digSum[n]]
44Ayush 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ủ Đề