Write the code to reverse a given number using Command Line Arguments.
Answer»
There is no need for a specific input line because the number is entered as a Command-line Argument.
From the command line argument, extract the input number. This number will be of the String type.
Convert this number to an integer and save it in the variable num.
Create a variable called rev to record the inverse of this number.
Now loop through num until it equals zero, i.e. (num > 0).
At the end of each iteration, add the REMAINDER of num to rev after multiplying it by 10. The last digit of num will be STORED in rev.
To ELIMINATE the last digit from the num, divide it by ten.
When the loop is finished, rev has the opposite of num.
class IB { // Function for reversing the number public static int revNum(int num) { // Variable which stores the // required reverse number int rev = 0; // Traversing the number digit by digit while (1) { if(num <= 0) break; // Attach the last digit of num // as the next digit of rev rev = rev * 10 + num % 10; // Drop the last digit of num num = num / 10; } // Return the resultant reverse number return rev; } public static void main(String[] args) { if (args.length > 0) { // Obtain the command line argument and // Convert it to integer type from string type int number = Integer.parseInt(args[0]); System.out.println(revNum(number)); } else System.out.println("No command line arguments found."); }}