How many 4 digit numbers with non zero digits have the sum of their digits is 12?

21
3
21
72
21
73
21
72
21
85
21
7
21
87
21
88
21
30
21
90

21
3
21
92
21
73
21
7
21
87
21
96
21
30
21
90

 

21
6
104
6
21
72

 

 

21
02

21
03
21
04
21
73
21
73
21
07
21
5

21
6
21
92
21
73
21
55

 

21
6
21
14

21
6
21
16
21
17

C#




21
18

21
03

using

21
21

 

21
2
21
07

21
6
21
1

21
6
21
11
21
7
21
8
21
7
21
0

21
6
21
2

21
3
21
7
21
5

 

21
3
21
7
21
8

21
9
104
0

21
9
104
2

21
3
104
4

 

21
3
104
6
104
7

21
6
104
4

 

21
6
Sum of digits is 15
1

21
6
21
4
21
11
21
48
21
57

21
6
21
2

21
3
21
7
Sum of digits is 15
9

 

21
3// C++ program to compute sum of digits in1

21
3
21
66

21
6
104
4

104
4

 

21
70

PHP




21
71

21
72

21
73

 

21
74

21
75

21
76
21
77
21
78
21
90

21
2

21
6
21
82
21
83

21
6
21
7 namespace5
21
78
21
88

21
6
21
2

21
3
21
82
21
73
21
82
21
85
21
78
21
97

21
3
21
78
21
73
21
78
104
02

21
6
104
4

21
6
104
6
21
82
21
22

104
4

 

104
10

21
78
104
12

 

// C++ program to compute sum of digits in1

104
14
104
15
21
78
104
17

104
18namespace5
104
20
104
17

 

104
22

104
23

104
24

Javascript




104
25

 

104
26

// number.

 

21
1

21
76
104
30

21
2

21
6
104
33
21
5

21
6
21
7
21
8

21
3
104
0

21
3
104
41

21
6
104
4

21
6
104
6
104
7

104
4

 

Sum of digits is 15
1

104
33
Sum of digits is 15
9

104
51

 

104
52

Output

21

Time Complexity: O(log N)
Auxiliary Space: O(1)

How to compute in a single line?

The below function has three lines instead of one line, but it calculates the sum in one line using for loop. It can be made one-line function if we pass the pointer to the sum. 

Below is the implementation of the above approach:

C++




#include

using namespace

21
0

 

21
1

21
2
21
3

21
4
21
5

21
6
21
7
21
8
21
7
21
0

21
6
21
2

21
3
21
7
104
7

 

21
3
104
73

21
3
104
75
104
76

21
9
21
22

 

21
3
104
6
104
7

21
6
104
4

Sum of digits is 15
0

 

Sum of digits is 15
1

21
7
Sum of digits is 15
3

21
2

21
6
Sum of digits is 15
6

21
6
21
7
Sum of digits is 15
9

 

21
6// C++ program to compute sum of digits in1

21
6// C++ program to compute sum of digits in3

21
6
104
6 // C++ program to compute sum of digits in6

104
4

// C++ program to compute sum of digits in8

C




// number.1

 

21
1

21
7
21
8
21
7
21
0

21
2

21
6
21
7
104
7

 

21
6
104
73

21
6
104
75
104
76

21
3
21
22

 

21
6
104
6
104
7

104
4

 

Sum of digits is 15
1

21
7
Sum of digits is 15
3

21
2

21
6
21
7
Sum of digits is 15
9

 

21
6// C++ program to compute sum of digits in1

21
6namespace4namespace5namespace6namespace7

21
6
104
6 // C++ program to compute sum of digits in6

104
4

Java




21
02

21
03

21
04
21
05

 

21
2
21
07

 

21
6
21
1

21
6
21
11
21
7
21
8
21
7
21
0

21
6
21
2

21
3
21
7
104
7

 

21
3
104
73

21
3
104
75
Sum of digits is 15
65
21
21
Sum of digits is 15
67
21
21
Sum of digits is 15
69
21
30
Sum of digits is 15
71
21
30
21
90

21
9
21
22

 

21
3
104
6
104
7

21
6
104
4

 

21
6
Sum of digits is 15
1

21
6
21
4
21
11
21
48
21
49

21
6
21
2

21
3
21
7
21
54
21
55
21
22

 

21
3// C++ program to compute sum of digits in1

21
3
21
60

21
6
104
4

104
4

 

21
64

Python3




21
68

 

 

21
69
21
70

 

21
6
21
72
21
73
21
21

 

21
6// C++ program to compute sum of digits in11

21
6
21
7// C++ program to compute sum of digits in14
21
21
21
80

21
3
21
72
21
85
21
73
21
7
21
87
21
88
21
30
21
90

21
3
21
92
21
73
21
7
21
87
21
96
21
30
21
90

 

21
6
104
6
21
72

 

 

21
02

21
03
21
04
21
73
21
73
21
07
21
5

21
6
21
92
21
73
21
55

 

21
6
21
14

21
6
21
16
21
17

 

// C++ program to compute sum of digits in53

// C++ program to compute sum of digits in54

C#




21
18

21
03

using

21
21

 

21
2
21
07

21
6
21
11
21
7
21
8
21
7
21
0

21
6
21
2

21
3
21
7
104
7

 

21
3
104
73

21
3
104
75
104
76

21
9
21
22

 

21
3
104
6
104
7

21
6
104
4

 

21
6
Sum of digits is 15
1

21
6
21
4
21
11
21
48
21
57

21
6
21
2

21
3
21
7
Sum of digits is 15
9

 

21
3// C++ program to compute sum of digits in1

21
3
21
66

21
6
104
4

104
4

 

21
70

PHP




21
71

// number.05

// number.06

 

// number.07

21
76
21
77
21
78
21
90

21
2

 

21
6// number.14

21
6
104
75 namespace5
21
82
21
83
21
78 // number.21
21
82 // number.23
21
78 // number.25

// number.26

21
78 // number.28

21
6
104
6
21
82
21
22

104
4

 

104
10

21
78
104
12

 

// C++ program to compute sum of digits in1

104
18// number.39
21
78// number.41

 

104
22

104
23

104
24

Javascript




104
25

 

// number.46

21
03

 

// number.07

21
76
104
30

21
2

21
6// number.53

 

21
6// number.55

21
6
104
75// number.58

21
3// number.60

21
3// number.62

21
3
21
22

21
6
104
6
104
7

104
4

 

Sum of digits is 15
1

// number.70

 

104
51

 

// number.72

 

104
52

Output

21

Time Complexity: O(log N)
Auxiliary Space: O(1)

Sum of the digits of a given number using recursion:

Follow the below steps to solve the problem:

  • Get the number
  • Get the remainder and pass the next remaining digits
  • Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and adding it to the sum.
  • Divide the number by 10 with help of the ‘/’ operator to remove the rightmost digit.
  • Check the base case with n = 0
  • Print or return the sum

Below is the implementation of the above approach:

C++




// number.74

21
03

// number.76

using namespace

21
0

21
2
21
3

21
4
21
5

21
6
21
7 // number.86
21
7 // number.88

21
6
21
2

21
3
21
03 // number.93

21
9
104
6 // C++ program to compute sum of digits in6

21
3
104
4

 

21
3
104
6 #include 01

21
6
104
4

Sum of digits is 15
0

 

Sum of digits is 15
1

21
7 #include 07
21
48
21
90

21
2

21
6
Sum of digits is 15
6

 

21
6// C++ program to compute sum of digits in1

21
6#include 16

21
6
104
6 // C++ program to compute sum of digits in6

104
4

C




#include 21

21
03

// number.1

 

21
7 // number.86
21
7 // number.88

21
2

21
6
21
03 // number.93

21
3
104
6 // C++ program to compute sum of digits in6

21
6
104
4

 

21
6
104
6 #include 01

104
4

 

Sum of digits is 15
1

21
7
Sum of digits is 15
3

21
2

21
6// C++ program to compute sum of digits in1

21
6namespace4namespace5#include 50#include 51

21
6
104
6 // C++ program to compute sum of digits in6

104
4

Java




21
02

21
03

21
04
21
05

 

21
2
21
07

 

21
6
21
1

21
6
21
11
21
7 // number.86
21
7 // number.88

21
6
21
2

21
3
21
03 #include 74
21
21
21
27

21
9
104
6
21
21
21
22

21
3
104
4

 

21
3
104
6 #include 85
21
30#include 87
21
30
104
17

21
6
104
4

 

21
6
Sum of digits is 15
1

21
6
21
4
21
11
21
48
21
49

21
6
21
2

21
3// C++ program to compute sum of digits in1

21
3using04
21
55// number.41

21
6
104
4

104
4

 

21
64

Python3




using11

using12

 

 

21
69 using14

21
6
104
6
21
21
21
03 using19
21
73
21
73
21
21 using23
21
7using25
21
88
21
30
21
90
21
85 // number.86
21
7using25
21
96
21
30using35

 

 

21
02

21
03
21
04
21
73
21
73
21
07
21
5

 

21
6
21
14

21
6
21
16using47
21
55using35

 

// C++ program to compute sum of digits in53

// C++ program to compute sum of digits in54

C#




21
18

21
03

using

21
21

 

21
2
21
07

21
6
21
1

21
6
21
11
21
7 // number.86
21
7 // number.88

21
6
21
2

21
3
104
6 using70

21
6
104
4

 

21
6
Sum of digits is 15
1

21
6
21
4
21
11
21
48
21
57

21
6
21
2

21
3// C++ program to compute sum of digits in1

21
3using85

21
6
104
4

104
4

 

21
70

PHP




21
71

using91

21
03

21
76 // number.86using95
21
90

21
2

104
6 using95 namespace00using95 namespace02

namespace03// number.86using95 namespace06

104
4

 

104
10

 

// C++ program to compute sum of digits in1

104
18 namespace11

 

namespace12

104
24

Javascript




104
25

namespace15

namespace16

namespace17// number.07

namespace19 

namespace20

21
76 namespace22

namespace20

21
2

21
3
21
03namespace27

namespace28

104
6 namespace30

21
3
104
4

 

21
3
104
6 namespace35

namespace36

104
4

namespace36 

Sum of digits is 15
1

namespace36namespace41

namespace36 

namespace43

104
52

Output

21

Time Complexity: O(log N)
Auxiliary Space: O(log N)

Sum of the digits of a given number with input as string:

When the number of digits of that number exceeds 1019 , we can’t take that number as an integer since the range of long long int doesn’t satisfy the given number. So take input as a string, run a loop from start to the length of the string and increase the sum with that character(in this case it is numeric)

Follow the below steps to solve the problem:

  • Declare a variable sum equal to zero
  • Run a loop from zero to the length of the input string
    • Add the value of each character into the sum, by converting the character into it’s integer value
  • Return sum

Below is the implementation of the above approach:

C++14




namespace45

#include

using namespace

21
0

 

21
7 namespace51

21
2

21
6
21
7
21
5

 

21
6namespace57

21
6
104
75 namespace5
21
7 namespace62

21
3namespace64

21
3namespace66

21
3namespace68

21
3namespace70

21
6
104
4

21
6
104
6
104
7

104
4

 

104
10

21
7
Sum of digits is 15
3

21
2

21
6namespace82namespace83
21
22

 

21
6// C++ program to compute sum of digits in1

21
6namespace88

21
6
104
6 // C++ program to compute sum of digits in6

104
4

Java




namespace93

21
04
21
05

21
2
21
07

 

21
6
21
11
21
7
21
001

21
6
21
2

21
3
21
7
21
20
21
21
21
22

 

21
3namespace57

21
3
104
75 namespace5
21
7
21
015
21
21
21
017

 

21
9namespace64

21
9namespace66

21
9namespace68

21
9
21
025
21
026
21
22

21
3
104
4

21
3
104
6
104
7

21
6
104
4

 

21
6
104
10

21
6
21
4
21
11
21
48
21
49

21
6
21
2

21
3
21
045namespace83
21
22

 

21
3// C++ program to compute sum of digits in1

21
3
21
051

21
6
104
4

104
4

 

21
055

Python3




21
056

21
69
21
70

21
6
21
060

21
6
21
72
21
73
21
21

21
6
21
066

21
6
104
75
21
069
21
070
21
071

21
3
21
073

21
3
21
72
21
73
21
72
21
85
21
7
21
080

 

21
6
104
6
21
72

 

21
02

 

 

21
03
21
04
21
73
21
73
21
07
21
5

21
6
21
92
21
73 namespace83

 

21
6
21
14

21
6
21
16
21
17

C#




21
100

using

21
21

21
4
21
2
21
07

21
6
21
11
21
7
21
001

21
6
21
2

21
3
21
7
21
5

 

21
3namespace57

21
3
104
75 namespace5
21
7
21
121

 

21
9namespace64

21
9namespace66

21
9namespace68

21
9namespace70

21
3
104
4

21
3
104
6
104
7

21
6
104
4

 

21
6
104
10

21
6
21
11
21
4
21
48
21
57

21
6
21
2

21
3
21
045namespace83
21
22

 

21
3// C++ program to compute sum of digits in1

21
3
21
153

21
6
104
4

104
4

 

21
055

PHP




21
71

21
159
21
160

// number.05

// number.06

 

// number.07

21
76
21
77
21
166
21
90

21
2

 

namespace36

21
82
21
83

namespace17 

namespace17namespace57

21
3
104
75 namespace5
21
178
21
83
21
178
21
181
21
182namespace5
21
166
104
17
21
178
21
187

 

21
9
21
189

21
9
21
82
21
73
21
82
21
194
21
166
21
196
21
178
21
198

21
3
104
4

namespace17 

21
6
104
6
21
82
21
22

104
4

 

104
10

21
166
21
73namespace83
21
22

 

// C++ program to compute sum of digits in1

104
18// number.39
21
166// number.41

 

21
217

 

104
24

Javascript




104
25

21
220

 

21
76
21
222

21
2

21
6
21
225

 

21
6namespace57

21
6
104
75
21
230

21
6
21
2

21

21
3namespace64

21
3namespace66

21
3namespace68

21
3
21
241

21
6
104
4

21
6
104
6
104
7

104
4

 

104
10

21
249namespace83
21
22

21
252

 

21
253

104
52

Output

104

Time Complexity: O(log N)
Auxiliary Space: O(1)

Sum of the digits of a given number using tail recursion:

Follow the below steps to solve the problem:

  • Add another variable “Val” to the function and initialize it to ( Val = 0 )
  • On every call to the function add the mod value (n%10) to the variable as “(n%10)+val” which is the last digit in n. Along with passing the variable n as n/10. 
  • So on the First call, it will have the last digit. As we are passing n/10 as n, It follows until n is reduced to a single digit. 
  • n<10 is the base case so When n < 10, then add the n to the variable as it is the last digit and return the val which will have the sum of digits

Below is the implementation of the above approach:

C++




21
255

#include

using namespace

21
0

 

21
260

21
7
21
262
21
7
21
264
21
7
21
266

21
2

21
6
21
03
21
270

21
3
21
272

21
3
104
6
21
275

21
6
104
4

21
6
104
6
21
280

104
4

 

Sum of digits is 15
1

21
7
Sum of digits is 15
3

21
2

21
6
21
7
21
288

 

21
6// C++ program to compute sum of digits in1

21
6
21
7
21
293

21
6
21
295
21
296
21
297

21
6
104
6 // C++ program to compute sum of digits in6

104
4

 

// number.72

C




21
303

// number.1

 

21
260

21
7
21
262
21
7
21
264
21
7
21
266

21
2

21
6
21
03
21
270

21
3
21
272

21
3
104
6
21
275

21
6
104
4

21
6
104
6
21
280

104
4

 

Sum of digits is 15
1

21
7
Sum of digits is 15
3

21
2

21
6
21
7
21
288

 

21
6// C++ program to compute sum of digits in1

21
6
21
7
21
293

21
6namespace4namespace5
21
342
21
343

21
6
104
6 // C++ program to compute sum of digits in6

104
4

 

21
348

Java




21
349

21
04
21
05

21
04
21
353

21
04
21
355

 

21
2
21
357

 

21
6
21
359

21
6
21
361

21
6
21
11
21
7
21
262
21
7
21
264
21
7
21
266

21
6
21
2

21
3
21
03
21
374
21
30
21
27

21
9
21
272

21
9
104
6
21
275

21
3
104
4

21
3
104
6
21
386
21
30
21
388
21
30
21
390

21
6
104
4

 

21
6
21
394

21
6
21
4
21
11
21
48
21
399

21
6
21
2

21
3
21
7
21
404
21
405
21
22

 

21
3// C++ program to compute sum of digits in1

21
3
21
7
21
411
21
21
104
17

21
3
21
415
21
296
21
417

21
6
104
4

104
4

Python3




21
421

 

21
422

21
423

 

 

21
69
21
425

 

21
6
21
03
21
374
21
30
21
80

21
3
21
432
21
73
21
432
21
85
21
92

21
3
104
6
21
432

 

21
6
104
6
21
442
21
96
21
96
21
30
21
446
21
88
21
30
21
90
21
85
21
266

 

21
02

 

 

21
03
21
04
21
73
21
73
21
07
21
5

21
6
21
460
21
73
21
405

 

21
6
21
14

21
6
21
466
21
73
21
468
21
21
21
90

 

21
6
21
16namespace5
21
474
21
475

 

21
476

C#




21
477

using

21
21

 

21
2
21
07

 

21
6
21
359

21
6
21
361

21
6
21
11
21
7
21
262
21
7
21
264
21
7
21
266

21
6
21
2

21
3
21
03
21
270

21
9
21
272

21
9
104
6
21
275

21
3
104
4

21
3
104
6
21
280

21
6
104
4

 

21
6
Sum of digits is 15
1

21
6
21
4
21
11
21
48
21
57

21
6
21
2

21
3
21
7
21
288

 

21
3// C++ program to compute sum of digits in1

21
3
21
7
21
293

 

21
3
21
529
21
296
21
417

21
6
104
4

104
4

 

// number.72

Javascript




104
25

 

21
537

 

21
359

21
361

21
76
21
541

21
2

21
6
21
03
21
545

21
6
21
2

21
3
21
272

21
3
104
6
21
275

21
6
104
4

21
6
104
6
21
557

21
6
21
559

104
4

 

Sum of digits is 15
1

21
6
21
563

21
6
21
565

21

21
6
21
568
21
296
21
417

 

// number.72

 

104
52

Output

Sum of digits is 15

Time Complexity: O(log N)
Auxiliary Space: O(log N)

Please write comments if you find the above codes/algorithms incorrect, or find better ways to solve the same problem.

What 4 numbers add up to 12?

4+8. 5+7. 1+2+9. 1+3+8.

How many 4

No. of numbers that can be formed using all four digits 1,2,3,4=4! =4×3×2×1=24.

How many 4

This is 13 choose 3, but there are overcounts when 10 and 11 are a "digit." There are 4 possibilities when 11 is the first "digit" or 10 as the last 3 digits. there are 3 choose 2 possibilities. This is 286−7=279.

How many 4

Answer. There are 219 4-digit numbers whose sum of digits is 10.