Question

In: Computer Science

Doing recursion to match digits of two inputted integers. Found this online and it works but...

Doing recursion to match digits of two inputted integers. Found this online and it works but I wanna understand how each line works. Like why x %10 == y%10 and calling DigitMatch(x/10,y/10)?

public int digitMatch(int x, int y){

    if(x < 0 || y < 0){

      throw new IllegalArgumentException();

    }

      else if(x < 10 || y < 10){

         if(x % 10 == y % 10)

            return 1;

         else

             return 0;

      } else if( x % 10 == y % 10){

         return 1 + digitMatch(x/10, y/10);

      }

    else{

         return digitMatch(x/10,y/10);

    }

Solutions

Expert Solution

this program number of digits of two inputted int x and y matches .
this program starts with left most digit of in x and y.
and keep traversing right(digit by digit) in each recursive call.
if in any recursive call it found unmatch it return 0 .
else return 1(count increased by 1)

x%10 gives remainder when x is divided by 10
x/10 gives quotient when x is divided by 10

for example x=121 any y=221
call digitMatch(x/10,y/10) => digitMatch(121,221)

it starts with left most digit of x and y;
x%10 = 1
and y%10 = 1
and here (x%10==y%10)return 1

now we need to check whether next onward digits are same or not
means now we will check
x/10 = 12
y/10 = 22
here we call recursively 1+ digitMatch(x/10,y/10) => 1+ digitMatch(12,22)

next call:
x%10 = 2
and y%10 = 2
and here (x%10==y%10)return 1

now we will check number of digits matched in
x/10 = 1
y/10 = 2
here we call recursively return 1+digitMatch(x/10,y/10) => 1+ digitMatch(1,2)

next call:
x%10 = 1
and y%10 = 2
and here (x%10!=y%10)return 0
here x <10 and y<10
return 0 (No more call)

total match = 1+1+0 = 2 digits matched


Related Solutions

Researchers at two universities found that airlines are doing a better job of getting passengers to...
Researchers at two universities found that airlines are doing a better job of getting passengers to their destinations on time. Company A and Company B Airlines were among the leaders in on-time arrivals with both having 88% of their flights arriving on time. But for the 12% of flights that were delayed, how many minutes were these flights late? Sample data showing the number of minutes that delayed flights were late are shown below for both airlines. Use Excel to...
Pick an online source, a textbook, and two other sources and create a works cited page...
Pick an online source, a textbook, and two other sources and create a works cited page in MLA format. Thank you.
C++ Write a program that reads in two Integers. A starting number and a number of iterations. For each iteration, you generate a new number that is the result of reading off the number of same digits in the original number. output each Iteration
C++ Write a program that reads in two Integers. A starting number and a number of iterations. For each iteration, you generate a new number that is the result of reading off the number of same digits in the original number. output each Iteration.   Example starting number 331 next number [iteration 1]:"2 threes and 1 one" = 231 next number [iteration 2]:"1 two, 1 three, 2 ones" = 121321 next number [iteration 3]: "1 one, 1 two, 1 one,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT