- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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.

#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; }

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

Numbers with digit sum N and divisible by M: 4

- Related Questions & Answers
- Count numbers in range that are divisible by all of its non-zero digits in C++
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 in C++
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Find M-th number whose repeated sum of digits of a number is N in C++
- Count of numbers from range[L, R] whose sum of digits is Y in C++
- n-th number whose sum of digits is ten in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Find N digits number which is divisible by D in C++
- N digit numbers divisible by 5 formed from the M digits in C++
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Count of Numbers in Range where the number does not contain more than K non zero digits in C++
- Total number of non-decreasing numbers with n digits
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- C Program to check if a number is divisible by sum of its digits
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N in C++

Advertisements