How many 4 digit numbers with non zero digits have the sum of their digits is 12?
213 2172 2173 2172 2185 217 2187 2188 2130 2190 Show 213 2192 2173 217 2187 2196 2130 2190
216 1046 2172
2102 2103 2104 2173 2173 2107 215 216 2192 2173 2155
216 2114 216 2116 2117 C#2118 2103
2121
212 2107 216 211 216 2111 217 218 217 210 216 212 213 217 215
213 217 218 219 1040 219 1042 213 1044
213 1046 1047 216 1044
216 Sum of digits is 151 216 214 2111 2148 2157 216 212 213 217 Sum of digits is 159
213 // C++ program to compute sum of digits in 1213 2166 216 1044 1044
2170 PHP2171 2172 2173
2174 2175 2176 2177 2178 2190 212 216 2182 2183 216 217 namespace 52178 2188 216 212 213 2182 2173 2182 2185 2178 2197 213 2178 2173 2178 10402 216 1044 216 1046 2182 2122 1044
10410 2178 10412
10414 10415 2178 10417 10418 namespace 510420 10417
10422 10423 10424 Javascript10425
10426
211 2176 10430 212 216 10433 215 216 217 218 213 1040 213 10441 216 1044 216 1046 1047 1044
Sum of digits is 151 10433 Sum of digits is 159 10451
10452 Output 21 Time Complexity: O(log N) 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++
210
211 212 213 214 215 216 217 218 217 210 216 212 213 217 1047
213 10473 213 10475 10476 219 2122
213 1046 1047 216 1044 Sum of digits is 150
Sum of digits is 151 217 Sum of digits is 153 212 216 Sum of digits is 156 216 217 Sum of digits is 159
216 // C++ program to compute sum of digits in 1216 // C++ program to compute sum of digits in 3216 1046 // C++ program to compute sum of digits in 61044
C
211 217 218 217 210 212 216 217 1047
216 10473 216 10475 10476 213 2122
216 1046 1047 1044
Sum of digits is 151 217 Sum of digits is 153 212 216 217 Sum of digits is 159
216 // C++ program to compute sum of digits in 1216 namespace 4namespace 5namespace 6namespace 7216 1046 // C++ program to compute sum of digits in 61044 Java2102 2103 2104 2105
212 2107
216 211 216 2111 217 218 217 210 216 212 213 217 1047
213 10473 213 10475 Sum of digits is 1565 2121 Sum of digits is 1567 2121 Sum of digits is 1569 2130 Sum of digits is 1571 2130 2190 219 2122
213 1046 1047 216 1044
216 Sum of digits is 151 216 214 2111 2148 2149 216 212 213 217 2154 2155 2122
213 // C++ program to compute sum of digits in 1213 2160 216 1044 1044
2164 Python32168
2169 2170
216 2172 2173 2121
216 // C++ program to compute sum of digits in 11216 217 // C++ program to compute sum of digits in 142121 2180 213 2172 2185 2173 217 2187 2188 2130 2190 213 2192 2173 217 2187 2196 2130 2190
216 1046 2172
2102 2103 2104 2173 2173 2107 215 216 2192 2173 2155
216 2114 216 2116 2117
C#2118 2103
2121
212 2107 216 2111 217 218 217 210 216 212 213 217 1047
213 10473 213 10475 10476 219 2122
213 1046 1047 216 1044
216 Sum of digits is 151 216 214 2111 2148 2157 216 212 213 217 Sum of digits is 159
213 // C++ program to compute sum of digits in 1213 2166 216 1044 1044
2170 PHP2171
2176 2177 2178 2190 212
216 // number. 14216 10475 namespace 52182 2183 2178 // number. 212182 // number. 232178 // number. 25
2178 // number. 28216 1046 2182 2122 1044
10410 2178 10412
10418 // number. 392178 // number. 41
10422 10423 10424 Javascript10425
2103
2176 10430 212 216 // number. 53
216 // number. 55216 10475 // number. 58213 // number. 60213 // number. 62213 2122 216 1046 1047 1044
Sum of digits is 151
10451
10452 Output 21 Time Complexity: O(log N) Sum of the digits of a given number using recursion:Follow the below steps to solve the problem:
Below is the implementation of the above approach: C++
2103
210 212 213 214 215 216 217 // number. 86217 // number. 88216 212 213 2103 // number. 93219 1046 // C++ program to compute sum of digits in 6213 1044
213 1046 #include 01216 1044 Sum of digits is 150
Sum of digits is 151 217 #include 072148 2190 212 216 Sum of digits is 156
216 // C++ program to compute sum of digits in 1216 #include 16216 1046 // C++ program to compute sum of digits in 61044 C
2103
217 // number. 86217 // number. 88212 216 2103 // number. 93213 1046 // C++ program to compute sum of digits in 6216 1044
216 1046 #include 011044
Sum of digits is 151 217 Sum of digits is 153 212 216 // C++ program to compute sum of digits in 1216 namespace 4namespace 5#include 50#include 51216 1046 // C++ program to compute sum of digits in 61044 Java2102 2103 2104 2105
212 2107
216 211 216 2111 217 // number. 86217 // number. 88216 212 213 2103 #include 742121 2127 219 1046 2121 2122 213 1044
213 1046 #include 852130 #include 872130 10417 216 1044
216 Sum of digits is 151 216 214 2111 2148 2149 216 212 213 // C++ program to compute sum of digits in 1213 using 042155 // number. 41216 1044 1044
2164 Python3
2169 using 14216 1046 2121 2103 using 192173 2173 2121 using 23 217 using 252188 2130 2190 2185 // number. 86217 using 252196 2130 using 35
2102 2103 2104 2173 2173 2107 215
216 2114 216 2116 using 472155 using 35
C#2118 2103
2121
212 2107 216 211 216 2111 217 // number. 86217 // number. 88216 212 213 1046 using 70216 1044
216 Sum of digits is 151 216 214 2111 2148 2157 216 212 213 // C++ program to compute sum of digits in 1213 using 85216 1044 1044
2170 PHP2171
2103 2176 // number. 86using 952190 212 1046 using 95 namespace 00using 95 namespace 02
1044
10410
10418 namespace 11
10424 Javascript10425
2176 namespace 22
212 213 2103 namespace 27
1046 namespace 30213 1044
213 1046 namespace 35
1044
Sum of digits is 151
10452 Output 21 Time Complexity: O(log N) Sum of the digits of a given number with input as string:
Follow the below steps to solve the problem:
Below is the implementation of the above approach: C++14
210
217 namespace 51212 216 217 215
216 namespace 57216 10475 namespace 5217 namespace 62213 namespace 64213 namespace 66213 namespace 68213 namespace 70216 1044 216 1046 1047 1044
10410 217 Sum of digits is 153 212 216 namespace 82namespace 832122
216 // C++ program to compute sum of digits in 1216 namespace 88216 1046 // C++ program to compute sum of digits in 61044 Java
2104 2105 212 2107
216 2111 217 21001 216 212 213 217 2120 2121 2122
213 namespace 57213 10475 namespace 5217 21015 2121 21017
219 namespace 64219 namespace 66219 namespace 68219 21025 21026 2122 213 1044 213 1046 1047 216 1044
216 10410 216 214 2111 2148 2149 216 212 213 21045 namespace 832122
213 // C++ program to compute sum of digits in 1213 21051 216 1044 1044
21055 Python321056 2169 2170 216 21060 216 2172 2173 2121 216 21066 216 10475 21069 21070 21071 213 21073 213 2172 2173 2172 2185 217 21080
216 1046 2172
2102
2103 2104 2173 2173 2107 215 216 2192 2173 namespace 83
216 2114 216 2116 2117 C#21100
2121 214 212 2107 216 2111 217 21001 216 212 213 217 215
213 namespace 57213 10475 namespace 5217 21121
219 namespace 64219 namespace 66219 namespace 68219 namespace 70213 1044 213 1046 1047 216 1044
216 10410 216 2111 214 2148 2157 216 212 213 21045 namespace 832122
213 // C++ program to compute sum of digits in 1213 21153 216 1044 1044
21055 PHP2171 21159 21160
2176 2177 21166 2190 212
2182 2183
213 10475 namespace 521178 2183 21178 21181 21182 namespace 521166 10417 21178 21187
219 21189 219 2182 2173 2182 21194 21166 21196 21178 21198 213 1044
216 1046 2182 2122 1044
10410 21166 2173 namespace 832122
10418 // number. 3921166 // number. 41
21217
10424 Javascript10425 21220
2176 21222 212 216 21225
216 namespace 57216 10475 21230 216 212 216 213 namespace 64213 namespace 66213 namespace 68213 21241 216 1044 216 1046 1047 1044
10410 21249 namespace 832122 21252
21253 10452 Output 104 Time Complexity: O(log N) Sum of the digits of a given number using tail recursion:Follow the below steps to solve the problem:
Below is the implementation of the above approach: C++21255
210
21260 217 21262 217 21264 217 21266 212 216 2103 21270 213 21272 213 1046 21275 216 1044 216 1046 21280 1044
Sum of digits is 151 217 Sum of digits is 153 212 216 217 21288
216 // C++ program to compute sum of digits in 1216 217 21293 216 21295 21296 21297 216 1046 // C++ program to compute sum of digits in 61044
C21303
21260 217 21262 217 21264 217 21266 212 216 2103 21270 213 21272 213 1046 21275 216 1044 216 1046 21280 1044
Sum of digits is 151 217 Sum of digits is 153 212 216 217 21288
216 // C++ program to compute sum of digits in 1216 217 21293 216 namespace 4namespace 521342 21343 216 1046 // C++ program to compute sum of digits in 61044
21348 Java21349 2104 2105 2104 21353 2104 21355
212 21357
216 21359 216 21361 216 2111 217 21262 217 21264 217 21266 216 212 213 2103 21374 2130 2127 219 21272 219 1046 21275 213 1044 213 1046 21386 2130 21388 2130 21390 216 1044
216 21394 216 214 2111 2148 21399 216 212 213 217 21404 21405 2122
213 // C++ program to compute sum of digits in 1213 217 21411 2121 10417 213 21415 21296 21417 216 1044 1044 Python321421
21422 21423
2169 21425
216 2103 21374 2130 2180 213 21432 2173 21432 2185 2192 213 1046 21432
216 1046 21442 2196 2196 2130 21446 2188 2130 2190 2185 21266
2102
2103 2104 2173 2173 2107 215 216 21460 2173 21405
216 2114 216 21466 2173 21468 2121 2190
216 2116 namespace 521474 21475
21476 C#21477
2121
212 2107
216 21359 216 21361 216 2111 217 21262 217 21264 217 21266 216 212 213 2103 21270 219 21272 219 1046 21275 213 1044 213 1046 21280 216 1044
216 Sum of digits is 151 216 214 2111 2148 2157 216 212 213 217 21288
213 // C++ program to compute sum of digits in 1213 217 21293
213 21529 21296 21417 216 1044 1044
Javascript10425
21537
21359 21361 2176 21541 212 216 2103 21545 216 212 213 21272 213 1046 21275 216 1044 216 1046 21557 216 21559 1044
Sum of digits is 151 216 21563 216 21565 216 216 21568 21296 21417
10452 Output Sum of digits is 15 Time Complexity: 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 4No. of numbers that can be formed using all four digits 1,2,3,4=4! =4×3×2×1=24.
How many 4This 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 4Answer. There are 219 4-digit numbers whose sum of digits is 10.
|