Difference of two large numbers | Returns true if str1 is smaller than str2 , else false . ; Calculate lengths of both string ; Function for finding difference of larger numbers ; Before proceeding further , make sure str1 is not smaller ; Take an empty string for storing result ; Calculate lengths of both string ; Initially take carry zero ; Traverse from end of both strings ; Do school mathematics , compute difference of current digits and carry ; Subtract remaining digits of str1 [ ] ; Remove preceding 0 's ; Reverse resultant string ; Driver code ; Function call
def isSmaller ( str1 , str2 ) : NEW_LINE INDENT n1 = len ( str1 ) NEW_LINE n2 = len ( str2 ) NEW_LINE if ( n1 < n2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n2 < n1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( n1 ) : NEW_LINE INDENT if ( str1 [ i ] < str2 [ i ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT elif ( str1 [ i ] > str2 [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def findDiff ( str1 , str2 ) : NEW_LINE INDENT if ( isSmaller ( str1 , str2 ) ) : NEW_LINE INDENT str1 , str2 = str2 , str1 NEW_LINE DEDENT Str = " " NEW_LINE n1 = len ( str1 ) NEW_LINE n2 = len ( str2 ) NEW_LINE diff = n1 - n2 NEW_LINE carry = 0 NEW_LINE for i in range ( n2 - 1 , - 1 , - 1 ) : NEW_LINE INDENT sub = ( ( ord ( str1 [ i + diff ] ) - ord ( '0' ) ) - ( ord ( str2 [ i ] ) - ord ( '0' ) ) - carry ) NEW_LINE if ( sub < 0 ) : NEW_LINE INDENT sub += 10 NEW_LINE carry = 1 NEW_LINE DEDENT else : NEW_LINE INDENT carry = 0 NEW_LINE DEDENT Str += chr ( sub + ord ( '0' ) ) NEW_LINE DEDENT for i in range ( n1 - n2 - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( str1 [ i ] == '0' and carry ) : NEW_LINE INDENT Str += '9' NEW_LINE continue NEW_LINE DEDENT sub = ( ord ( str1 [ i ] ) - ord ( '0' ) ) - carry NEW_LINE if ( i > 0 or sub > 0 ) : NEW_LINE INDENT Str += chr ( sub + ord ( '0' ) ) NEW_LINE DEDENT carry = 0 NEW_LINE DEDENT Str = Str [ : : - 1 ] NEW_LINE return Str NEW_LINE DEDENT str1 = "88" NEW_LINE str2 = "1079" NEW_LINE print ( findDiff ( str1 , str2 ) ) NEW_LINE