Count the number of carry operations required to add two numbers | Function to count the number of carry operations ; Initialize the value of carry to 0 ; Counts the number of carry operations ; Initialize len_a and len_b with the sizes of strings ; Assigning the ascii value of the character ; Add both numbers / digits ; If sum > 0 , increment count and set carry to 1 ; Else , set carry to 0 ; Driver code
< ? php function count_carry ( $ a , $ b ) { $ carry = 0 ; $ count = 0 ; $ len_a = strlen ( $ a ) ; $ len_b = strlen ( $ b ) ; while ( $ len_a != 0 $ len_b != 0 ) { $ x = 0 ; $ y = 0 ; if ( $ len_a > 0 ) { $ x = $ a [ $ len_a - 1 ] - '0' ; $ len_a -- ; } if ( $ len_b > 0 ) { $ y = $ b [ $ len_b - 1 ] - '0' ; $ len_b -- ; } $ sum = $ x + $ y + $ carry ; if ( $ sum >= 10 ) { $ carry = 1 ; $ count ++ ; } else $ carry = 0 ; } return $ count ; } $ a = "9555" ; $ b = "555" ; $ count = count_carry ( $ a , $ b ) ; if ( $ count == 0 ) echo "0 STRNEWLINE " ; else if ( $ count == 1 ) echo "1 STRNEWLINE " ; else echo $ count , " STRNEWLINE " ; ? >