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
function count_carry ( a , b ) { let carry = 0 ; let count = 0 ; let len_a = a . length , len_b = b . length ; while ( len_a != 0 len_b != 0 ) { let x = 0 , y = 0 ; if ( len_a > 0 ) { x = a [ len_a - 1 ] - ' ' ; len_a -- ; } if ( len_b > 0 ) { y = b [ len_b - 1 ] - ' ' ; len_b -- ; } let sum = x + y + carry ; if ( sum >= 10 ) { carry = 1 ; count ++ ; } else carry = 0 ; } return count ; } let a = " " , b = " " ; let count = count_carry ( a , b ) ; if ( count == 0 ) document . write ( " " + " " ) ; else if ( count == 1 ) document . write ( " " + " " ) ; else document . write ( count ) ;