85.6k views
6 votes
Write a recursive method called method3 that accepts an integer parameter and returns the integer reformed by repeating digits 1 and 2. For example, method3 (123456) will return 11223456 repeating only 1 and 2 and method3 (21) will return 2211. If the number is 0, return 0. You may assume that n is not negative

1 Answer

5 votes

Answer:

Step-by-step explanation:

The following is written in Java and creates only the method3 and returns a new method after doubling all of the 2's and 1's in the input.

public static int method3(int x) {

int finalValue = 0;

String number = String.valueOf(x);

for (int i = 0; i < number.length(); i++) {

if (number.charAt(i) == 1 || number.charAt(i) == 2) {

finalValue += Integer.valueOf(number.charAt(i));

finalValue += Integer.valueOf(number.charAt(i));

} else {

finalValue += Integer.valueOf(number.charAt(i));

}

}

return finalValue;

}

User Jarron
by
3.5k points