Simple Recursion (Multiplying by using Addition)
Please support our C++ advertiser: Programming Forums
Thread Solved
![]() |
•
•
Posts: 219
Reputation:
Solved Threads: 0
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
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
Cplusplus Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int multiply(int x, int y); int main() { int x; int y; cout<<"Please enter 2 positive integers to multiply"<<endl; cin>>x; cin>>y; cout<<x<<" * "<<y<<" = "<<multiply(x,--y)<<endl; system ("PAUSE"); return 0; } int multiply(int x, int y) { if (x == 0) { return 0; } if (x == 1) { return x; } if (x > 1) { return (x + multiply(x,--y)); } }
You test x, and only change y
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
![]() |
Other Threads in the C++ Forum
- Previous Thread: Help (link lists referent)
- Next Thread: Passing Linked Lists - Syntax
•
•
•
•
Views: 460 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode