Solved Problem - Compare Numbers
In this lesson, we'll discuss another string manipulation problem.
We'll cover the following
Problem statement
Given two very long integers and . You have to determine which is bigger or if they are equal. Leading zeroes are allowed.
Input format
The first line contains a non-negative integer, .
The second line contains a non-negative integer, .
, may contain leading zeroes. Each of them consists of no more than digits.
Output format
Print a single character:
A
- ifB
- if=
- If numbers are equal
Sample
Input 1
6
19
Output 1
B
Input 2
23567
0023534
Output 2
A
Input 3
63
0063
Output 3
=
Explanation
Sample 2: The numbers without leading zeros are 23
and 12
. We’ll print A
since .
Solution
First of all, the number of digits can be . So clearly, no integer data type is enough (int
is 10 digits). We have to process the input as a string.
Before comparing the string, we remove the trailing zeros from both the numbers.
Comparing two numbers as a string without leading zeros has three cases:
- - A is larger
- - B is larger
- - First digit from the left that is different. If all digits are same, print
=
.
Removing leading zeros
Removing the first character one by one in order to remove the leading zeroes will be an operation. A better way to do this would be to create a new string without leading zeros.
This would remove leading zeros in .
Get hands-on with 1400+ tech skills courses.