TCS Coding Practice Question | Reverse a Number
Given a number, the task is to reverse this number using Command Line Arguments.
Input: num = 12345 Output: 54321 Input: num = 786 Output: 687
- Since the number is entered as Command line Argument, there is no need for a dedicated input line
- Extract the input number from the command line argument
- This extracted number will be in String type.
- Convert this number into integer type and store it in a variable, say num
- Initialize a variable, say rev_num, to store the reverse of this number with 0
- Now loop through the number num till it becomes 0, i.e. (num > 0)
- In each iteration,
- Multiply rev_num by 10 and add remainder of num to it. This will store the last digit of num in rev_num
- Divide num by 10 to remove the last digit from it.
- After the loop has ended, rev_num has the reverse of num.
My Personal Notes arrow_drop_up