View Single Post
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Simple Recursion (Multiplying by using Addition)

 
0
  #1
Dec 4th, 2008
Hello all,

My objective is simple. I have to take in 2 positive integers and multiply them by using addition. This is a recursive problem.

Example of Output:


Please enter 2 positive integers to multiply:

4 2

4 * 2 = 8



The problem is whenever the user types in both numbers, the program window just disappears.

To receive my result, I am suppose to use something like:

x + multiply(x,--y);


Please help me with this...Thank you


  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int multiply(int x, int y);
  6.  
  7. int main()
  8. {
  9.  
  10. int x;
  11. int y;
  12.  
  13.  
  14. cout<<"Please enter 2 positive integers to multiply"<<endl;
  15. cin>>x;
  16. cin>>y;
  17.  
  18. cout<<x<<" * "<<y<<" = "<<multiply(x,--y)<<endl;
  19.  
  20.  
  21. system ("PAUSE");
  22. return 0;
  23. }
  24.  
  25.  
  26. int multiply(int x, int y)
  27. {
  28.  
  29.  
  30. if (x == 0)
  31. {
  32. return 0;
  33. }
  34.  
  35. if (x == 1)
  36. {
  37. return x;
  38. }
  39.  
  40. if (x > 1)
  41.  
  42. {
  43. return (x + multiply(x,--y));
  44. }
  45.  
  46. }
Reply With Quote