Count numbers in range 1 to N which are divisible by X but not by Y | C ++ implementation of above approach ; Function to count total numbers divisible by x but not y in range 1 to N ; Check if Number is divisible by x but not Y if yes , Increment count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( 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 X = 2 , Y = 3 , N = 10 ; cout << countNumbers ( X , Y , N ) ; return 0 ; }