Count numbers up to N having digit D in its octal representation | C ++ program to implement the above approach ; Function to count the numbers in given range whose octal representation contains atleast digit , d ; Store count of numbers up to n whose octal representation contains digit d ; Iterate over the range [ 1 , n ] ; Calculate digit of i in octal representation ; Check if octal representation of x contains digit d ; Update total ; Update x ; Print the answer ; Driver Code ; Given N and D ; Counts and prints numbers up to N having D as a digit in its octal representation
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countNumbers ( int n , int d ) { int total = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int x = i ; while ( x > 0 ) { if ( x % 8 == d ) { total ++ ; break ; } x = x / 8 ; } } cout << total ; } int main ( ) { int n = 20 , d = 7 ; countNumbers ( n , d ) ; return 0 ; }