Count numbers from 1 to n that have 4 as a digit | Returns sum of all digits in numbers from 1 to n ; One by one compute sum of digits in every number from 1 to n ; A utility function to compute sum of digits in a given number x ; Driver Code
< ? php function countNumbersWith4 ( $ n ) { for ( $ x = 1 ; $ x <= $ n ; $ x ++ ) $ result += has4 ( $ x ) ? 1 : 0 ; return $ result ; } function has4 ( $ x ) { while ( $ x != 0 ) { if ( $ x % 10 == 4 ) return true ; $ x = intval ( $ x / 10 ) ; } return false ; } $ n = 328 ; echo " Count ▁ of ▁ numbers ▁ from ▁ 1 ▁ to ▁ " . $ n . " ▁ that ▁ have ▁ 4 ▁ as ▁ a ▁ a ▁ digit ▁ is ▁ " . countNumbersWith4 ( $ n ) ; ? >