Sachin writes all the numbers from 1 to 1000 on a paper in order find the 2883 digit written by him
269308300None of these Show Answer : C Solution : Before 1000 there are one digit, two digits and three digits numbers. Given a number n, we need to find the sum of its digits such that: If n < 10 digSum(n) = n Else digSum(n) = Sum(digSum(n))Examples : Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum(x) == 10 Hence ans will be 1+0 = 1 Input : 5674 Output : 4A brute force approach is to sum all the digits until the sum < 10. Below is the brute force program to find the sum. C++#include using namespace std; int digSum(int n) { int sum = 0; while(n > 0 || sum > 9) { if(n == 0) { n = sum; sum = 0; } sum += n % 10; n /= 10; } return sum; } int main() { int n = 1234; cout << digSum(n); return 0; } Javaimport java.util.*; public class GfG { static int digSum(int n) { int sum = 0; while (n > 0 || sum > 9) { if (n == 0) { n = sum; sum = 0; } sum += n % 10; n /= 10; } return sum; } public static void main(String argc[]) { int n = 1234; System.out.println(digSum(n)); } } Pythonimport math def digSum( n): sum = 0 while(n > 0 or sum > 9): if(n == 0): n = sum sum = 0 sum += n % 10 n /= 10 return sum n = 1234 print (digSum(n)) C#using System; class GFG { static int digSum(int n) { int sum = 0; while (n > 0 || sum > 9) { if (n == 0) { n = sum; sum = 0; } sum += n % 10; n /= 10; } return sum; } public static void Main() { int n = 1234; Console.Write(digSum(n)); } } PHPfunction digSum( $n) { $sum = 0; while($n > 0 || $sum > 9) { if($n == 0) { $n = $sum; $sum = 0; } $sum += $n % 10; $n = (int)$n / 10; } return $sum; } $n = 1234; echo digSum($n); ?> Javascriptlet n = 1234; function getSum(n) { let sum = 0; while (n > 0 || sum > 9) { if(n == 0) { n = sum; sum = 0; } sum = sum + n % 10; n = Math.floor(n / 10); } return sum; } document.write(getSum(n)); C#include int digSum(int n) { int sum = 0; while(n > 0 || sum > 9) { if(n == 0) { n = sum; sum = 0; } sum += n % 10; n /= 10; } return sum; } int main() { int n = 1234; printf("%d",digSum(n)); return 0; } Output : 1Time Complexity: O(log(n)). So, another challenge is “Could you do it without any loop/recursion in O(1) runtime?” YES!! How does the above logic works? The logic behind this approach is : To check if a number is divisible by 9, add the digits of the number and check if the sum is divisible by 9 or not. If yes, is the case, the number is divisible by 9, otherwise, it’s not. let’s
take 27 i.e (2+7 = 9) hence divisible by 9. Therefore, The problem is widely known as the digit root problem. You may find this Wikipedia article useful. -> https://en.wikipedia.org/wiki/Digital_root Below is the implementation of the above idea : C++#include using namespace std; int digSum(int n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } int main() { int n = 9999; cout< return 0; } import java.io.*; class GFG { static int digSum(int n) { if (n == 0) return 0; return (n % 9 == 0) ? 9
: (n % 9); } public static void main (String[] args) { int n = 9999; System.out.println(digSum(n)); } } Python3def digSum(n): if (n == 0): return 0 if (n % 9 == 0): return 9 else: return (n % 9) n = 9999 print(digSum(n)) C#using System; class GFG { static int digSum(int n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } public static void Main () { int n = 9999; Console.Write(digSum(n)); } } PHPfunction digSum($n) { if ($n == 0) return 0; return ($n % 9 == 0) ? 9 : ($n % 9); } $n = 9999; echo digSum($n); ?> Javascriptfunction digSum(n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } n = 9999; document.write(digSum(n)); Output: 9Time Complexity: O(1) Auxiliary Space: O(1) Related Post : This article is contributed by Ayush 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. How many digits are used in writing the numbers from 1 to 1000?Now let us look at the numbers from 1 to 1000. To write all these numbers down, we need to use 9 + 180 + 2700 + 4 = 2893 digits to accommodate the 9 single-digit numbers, the 90 double-digit numbers, the 900 triple-digit numbers and the number 1000. How may zeros did we need?
How many digits are used to write numbers?Symbols, Digits, & Numerals
We have ten digits we use to make up all numerals. A numeral is a number written down. These digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. That's it!
How many digits is a thousand?What are the names of each digit places in a number?. |