Count numbers with difference between number and its digit sum greater than specific value in C++

C++Server Side ProgrammingProgramming

<p>We are provided two numbers N which defines a range [1,N] and D which is a difference.The goal is to find all the numbers in the range [1,N] such that the [ number - (sum of its digits) ] &gt; D. We will do this by traversing numbers from 1 to N and for each number we will calculate its digit sum using a while loop. Check if the number and calculated digit sum has a difference more than D.</p><p>Let&rsquo;s understand with examples.</p><p><strong>Input</strong>&nbsp;</p><pre class="result notranslate">N=15 D=5</pre><p><strong>Output</strong>&nbsp;</p><pre class="result notranslate">Numbers such that difference b/w no. and its digit sum greater than value D: 6</pre><p><strong>Explanation</strong>&nbsp;</p><pre class="result notranslate">Numbers 10, 11, 12, 13, 14, 15 satisfy the condition. ( 10-1, 11-2, 12-3, 13-4, 14-5, 15-6 ) all differences are 9 which is greater than 5.</pre><p><strong>Input</strong>&nbsp;</p><pre class="result notranslate">N=20 D=10</pre><p><strong>Output</strong>&nbsp;</p><pre class="result notranslate">Only 20 satisfies the condition. 20-2=18 &gt; 10.</pre><p><strong>Explanation</strong>&nbsp;</p><pre class="result notranslate">This is list of numbers divisible by all non-zero digits : 100 101 102 104 105 110 111 112 115 120 122 124 126 128 132 135 140 144 150 155 162 168 175 184 200</pre><h2>Approach used in the below program is as follows</h2><ul class="list"><li><p>We take integers N and D.</p></li><li><p>Function digitSum(int n, int d) takes variables N, D and returns the count of numbers with (num-digitsum) &gt;d.</p></li><li><p>Take the initial variable count as 0 for such numbers.</p></li><li><p>Take variable digsum as 0</p></li><li><p>Traverse range of numbers using for loop. i=1 to i=n</p></li><li><p>Now for each number num=i, using while loop check if number is &gt;0.</p></li><li><p>calculate digsum+=num%10. Reduce num=num/10 to add the next digit.</p></li><li><p>At the end of the while, check if ( i - digsum &gt; d ). If true increment count.</p></li><li><p>At the end of all loops count will have a total number which satisfies the condition.</p></li><li><p>Return the count as result.</p></li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/OkkygSu2" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include &lt;bits/stdc++.h&gt; using namespace std; int digitSum(int n, int d){ &nbsp; &nbsp;int count = 0; &nbsp; &nbsp;int digsum = 0; &nbsp; &nbsp;for (int i = 1; i &lt;= n; i++){ &nbsp; &nbsp; &nbsp; int num=i; &nbsp; &nbsp; &nbsp; digsum=0; &nbsp; &nbsp; &nbsp; while(num&gt;0){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;digsum+=num%10; //sum of digits &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num=num/10; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; if(i-digsum&gt;d) //original number is i { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//cout&lt;&lt;i&lt;&lt;&quot; &quot;; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return count; } int main(){ &nbsp; &nbsp;int N = 20; &nbsp; &nbsp;int D = 8; &nbsp; &nbsp;cout &lt;&lt;&quot;Numbers such that difference between number and its digit sum greater than specific value: &quot;&lt;&lt;digitSum(N,D); &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><p>If we run the above code it will generate the following output &minus;</p><pre class="result notranslate">Numbers such that difference between number and its digit sum greater than specific value: 11</pre>
raja
Updated on 31-Oct-2020 04:53:26

Advertisements