76.4k views
2 votes
Write a recursive function num_eights that takes a positive integer pos and returns the number of times the digit 8 appears in pos

User M B Parvez
by
3.6k points

1 Answer

4 votes

Answer:

Step-by-step explanation:

The following is written in Java. It creates the function num_eights and uses recursion to check how many times the digit 8 appears in the number passed as an argument. A test case has been created in the main method and the output can be seen in the image below highlighted in red.

public static int num_eights(int pos){

if (pos == 0)

return 0;

if (pos % 10 == 8)

return 1 + num_eights(pos / 10);

else

return num_eights(pos / 10);

}

Write a recursive function num_eights that takes a positive integer pos and returns-example-1
User Jahaad
by
3.5k points