69.3k views
3 votes
Two non-negative integers x and y are equal if either: Both are 0, or x-1 and y-1 are equal Write a boolean-method named equals that recursively determines whether its two int parameters are equal and returns true if they are and false otherwise.

1 Answer

6 votes

Answer:

Following are the code which is mention below.

bool equals (int x, int y) // method equals

(y<0) ) // check number if it is less then 0

return false; // return false

if( (x==0) && (y==0) ) // check condition if number is equal to 0

return true; // return true

return equals(x-1, y-1);

Step-by-step explanation:

  • Here we declared a function "equals" of type bool which means it either return "true" or "false".
  • Inside the function equals we check the condition which are mention above

1. if x and y both are less then 0 then it returns "false".

2. if x and y both are equal to 0 then it returns "true"

3. Otherwise it return x-1 and y-1.

User Rosales
by
6.0k points