85.7k 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
8.3k points

Related questions

asked Aug 17, 2021 34.5k views
LuckySlevin asked Aug 17, 2021
by LuckySlevin
8.0k points
1 answer
3 votes
34.5k views
asked Apr 5, 2023 174k views
Dmitry Spikhalsky asked Apr 5, 2023
by Dmitry Spikhalsky
8.0k points
1 answer
0 votes
174k views
1 answer
5 votes
28.6k views