- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

# Count numbers in range 1 to N which are divisible by X but not by Y in C++

We are provided a number N. The goal is to find the numbers that are divisible by X and not by Y and are in the range [1,N].

Let’s understand with examples.

**Input**

N=20 X=5 Y=20

**Output**

Numbers from 1 to N divisible by X not Y: 2

**Explanation**

Only 5 and 15 are divisible by 5 and not 10.

**Input**

N=20 X=4 Y=7

**Output**

Numbers from 1 to N divisible by X not Y: 5

**Explanation**

Numbers 4, 8, 12, 16 and 20 are divisible by 4 and not 7.

## Approach used in the below program is as follows

We take an integer N.

Function divisibleXY(int x, int y, int n) returns a count of numbers from 1 to N which are divisible by X and not Y.

Take the initial variable count as 0 for such numbers.

Traverse range of numbers using for loop. i=1 to i=n

Now for each number i, check if ( i%x==0 && i%y!=0 ), if true increment count.

Return the count as result.

## Example

#include <bits/stdc++.h> using namespace std; int divisibleXY(int x, int y, int n){ int count = 0; for (int i = 1; i <= n; i++) { if(i%x==0 && i%y!=0 ) { count++; } } return count; } int main(){ int N = 100; int X=6, Y=8; cout <<"Numbers from 1 to N which are divisible by X and not Y: "<< divisibleXY(X,Y,N); return 0; }

## Output

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

Numbers from 1 to N which are divisible by X and not Y: 12

- Related Articles
- Sum of first N natural numbers which are divisible by X or Y
- Find permutation of n which is divisible by 3 but not divisible by 6 in C++
- Count numbers in a range that are divisible by all array elements in C++
- Count n digit numbers divisible by given number in C++
- Count the numbers divisible by ‘M’ in a given range in C++
- Count numbers which are divisible by all the numbers from 2 to 10 in C++
- Count pairs of numbers from 1 to N with Product divisible by their Sum in C++
- Count numbers in range that are divisible by all of its non-zero digits in C++
- C++ Program to count ordinary numbers in range 1 to n
- Find the Numbers that are not divisible by any number in the range [2, 10] using C++
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- Count all prefixes of the given binary array which are divisible by x in C++
- Prove that, if $x$ and $y$ are both odd positive integers, then $x^2 + y^2$ is even but not divisible by $4$.
- If a number is divisible by 3 need it to be tested for 9? Justify your answer by stating any 2 numbers which are divisible by 3 but not by 9.
- Smallest possible number divisible by all numbers from 1 to n in JavaScript

Advertisements