CS 2130 Computational structures Write Java programs that use command-line input
ID: 3908877 • Letter: C
Question
CS 2130 Computational structures
Write Java programs that use command-line input and call the following integer functions. All calculations for each function should be encapsulated within the function. Use a long datatype instead of int for all input and output variables. Do NOT use string variables in any of the algorithms. A sample main program will be provided as an example.
Turn in source code for each function, along with test output.For each of the above functions, state whether or not the function is one-to-one. Justify your answers. If a function is not one-to-one, show two inputs that have the same output.
2. ReverseDigits: The input variable X is a positive integer. The function should return as Y an integer containing the digits of X in reverse order. Leading zeros do not appear in X or Y. For example, if X is 2645, the Y is 5462. If X is 2400, then Y is 42. If Y = X, then X is called a palindrome (symmetric). Test your function with the following inputs:X = 45600 X = 62826 X = 157910 X = 483047 X = 901108
Explanation / Answer
import java.util.Scanner; public class ReverseDigits { public static long ReverseDigits(long num) { long result = 0; while (num > 0) { result *= 10; result += (num % 10); num /= 10; } return result; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number to be reversed: "); long num = in.nextLong(); System.out.println("The reverse of " + num + " is " + ReverseDigits(num)); } }