132k views
2 votes
Write biggerDigits.cpp that has a function called biggerDigits that uses two positive integer parameters with the same number of digits and returns an integer whose digit in each position is the bigger of the two digits in that position in the input parameters. If a negative parameter is given, or if parameters with unequal numbers of digits are given your function can return any result of your choosing. Use recursion.

cout << biggerDigits(567, 765) << endl; // prints 767
cout << biggerDigits(123456, 444444) << endl; // prints 444456
cout << biggerDigits(999, 111) << endl; // prints 999

1 Answer

6 votes

Answer:

Following are the program in the C++ Programming Language:

#include <iostream> //header file

using namespace std; //namespace

int biggerDigits(int n, int m) { //define function

if (n <= 0 || m <= 0) { //set if statement

return 0;

}

else { //set else statement

int a,b; //set integer variables

a = n % 10; //perform modulatio

b = m % 10; //perform modulatio

int max = a; //set integer variable and assign value

if (b > max) //set if statement

max = b; //initialize value to max

/*solution is here*/

return 10 * biggerDigits(n / 10, m / 10) + max;

}

}

int main() // main method

{ // code is refer from question to make the program

cout << biggerDigits(567, 765) << endl; // call the function biggerdigits

cout << biggerDigits(123456, 444444) << endl; // call the function biggerdigits

//call function and print result

cout << biggerDigits(999, 111) << endl; // call the function biggerdigits

return 0; // return the integer 0

}

Step-by-step explanation:

Here, we set the header file "<iostream>" and namespace "std" then, we set a function "biggerDigits()" and pass two integer type arguments inside it.

  • we set if condition in which when the variable "n" is less than equal to 0 or m is less than equal to 0 then, it return 0.
  • we set the else statement inside it we set two integer type variables "a" and "b" then, perform modulation with both the variables.
  • we set integer variable "max" and assign value of a in it.
  • we set if condition in which we check when the variable b is greater than variable max then, initialize the value of variable b in the variable max and return the solution through recursion.
User Bruno Tremblay
by
6.4k points