Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++

C++Server Side ProgrammingProgramming

We are provided two numbers START and END to define a range of numbers.The goal is to find all the numbers in the range [START,END] which have no digit as 0 and have sum of digits equal to a given number N. Also the numbers are divisible by M

We will do this by traversing numbers from START to END and for each number we will count the sum of its digit using a while loop ( only if all digits are non zero ). If this sum is equal to N and the number is divisible by M, increment count.

Let’s understand with examples.

Input 

START=1 END=100 N=9 M=6

Output 

Numbers with digit sum N and divisible by M: 4

Explanation 

Numbers 18, 36, 54, 72 have digit sum=9 and divisible by 6. None has 0 as a digit.

Input 

START=100 END=200 N=10 M=2

Output 

Numbers with digit sum N and divisible by M: 4

Explanation 

Numbers 118, 136, 154, 172 have digit sum=10 and divisible by 2. None has 0 as a digit.

Approach used in the below program is as follows

  • We take integers START, END, N and M.

  • Function digitSum(int start, int end, int n, int m) returns the count of numbers with digitsum=n and divisible by m and have all non-zero digits.

  • Take the initial variable count as 0 for such numbers.

  • Take variable digsum as 0

  • Take variable flag as 0.

  • Traverse range of numbers using for loop. i=start to i=end

  • Now for each number num=i, if num%m==0 ( divisible by m ) move forward.

  • using while loop check if number is >0. And find digits.

  • digit=num%10. If digit is non-zero calculate digsum+=digit. Reduce num=num/10 to add the next digit. If any digit is 0 set flag=0 and break the while loop

  • At the end of the while, check if ( digsum == n and flag==1 ). If true increment count.

  • Now increment i by m ( in multiples of m ).

  • At the end of all loops count will have a total number which satisfies the condition.

  • Return the count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int digitSum(int start, int end, int n, int m){
   int count = 0;
   int digsum = 0;
   int flag=0;
   for (int i = start; i <= end; i++){
      int num=i;
      digsum=0;
      flag=0;
      if(num%m==0){
         while(num>0){
            int digit=num%10;
            if(digit==0){
               flag=0;
               break;
            }
            digsum+=num%10; //sum of digits
            num=num/10;
            flag=1;
         }
         if(digsum==n && flag==1) //original number is i {
            count++;
            cout<<i<<" ";
         }
         i+=m; //now increment in multiples of m
         i--; // for loop has i++
      }
   }
   return count;
}
int main(){
   int START = 1;
   int END = 100;
   int N = 9;
   int M = 6;
   cout <<"Numbers with digit sum N and divisible by M: "<<digitSum(START,END,N, M);
   return 0;
}

Output

If we run the above code it will generate the following output −

Numbers with digit sum N and divisible by M: 4
raja
Published on 31-Oct-2020 09:28:59
Advertisements