Question

Write a recursive function named multiply that takes two positive integers as parameters and returns the...

Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 * 1 = 7 So, 7 * 4 = 7 + (7 + (7 + (7))) The file must be named: multiply.cpp

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

#include<iostream>

using namespace std;

//recursive function multiply

int multiply(int m, int n)

{

  int result;

  if (n == 1)

    result = m;

  else

    result = m + multiply(m, n-1);

  return(result);

}

//main function

int main()

{

int a, b;

cout<<"Enter the value of a and b: ";

cin>>a>>b;

cout<<"Result of multiplication is: "<<multiply(a, b);

return (0);

}

Output:

Please give thumbsup, if you like it. Thanks.

Add a comment
Know the answer?
Add Answer to:
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT