
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++
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
#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
- Related Articles
- Count numbers in range that are divisible by all of its non-zero digits in C++
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 in C++
- Count of numbers from range[L, R] whose sum of digits is Y in C++
- Find M-th number whose repeated sum of digits of a number is N in C++
- n-th number whose sum of digits is ten in C++
- Count of Numbers in Range where the number does not contain more than K non zero digits in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- C++ program to find largest or equal number of A whose sum of digits is divisible by 4
- Total number of non-decreasing numbers with n digits
- 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++
- Check whether product of digits at even places is divisible by sum of digits at odd place of a numbers in Python
- Sum of a two-digit number and the number obtained by reversing the digits is always divisible by?
