Count numbers up to N having digit D in its octal representation | Java 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
import java . io . * ; import java . util . * ; class GFG { static 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 ; } } System . out . println ( total ) ; } public static void main ( String [ ] args ) { int n = 20 , d = 7 ; countNumbers ( n , d ) ; } }