First occurrence of a digit in a given fraction | function to print the first digit ; reduce the number to its mod ; traverse for every decimal places ; get every fraction places when ( a * 10 / b ) / c ; check if it is equal to the required integer ; mod the number ; Driver Code
< ? php function first ( $ a , $ b , $ c ) { $ a %= $ b ; for ( $ i = 1 ; $ i <= $ b ; $ i ++ ) { $ a = $ a * 10 ; if ( $ a / $ b == $ c ) return $ i ; $ a %= $ b ; } return -1 ; } $ a = 1 ; $ b = 4 ; $ c = 5 ; echo first ( $ a , $ b , $ c ) ; ? >