Use the shift operators.
http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/
Multiplying by a factor of 2 is much easier than multiplying by 100. Here's a basic example of how to use shift operators.
#include <iostream>
using namespace std;
int main ()
{
int a = 12; // a = 12
cout << a << endl;
a = a >> 1; // a = 12 / 2 = 6
cout << a << endl;
a = a << 3; // a = 6 * 8 = 48
cout << a << endl;
cin.get ();
return 0;
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
im not thinking at all.... all i can do is say
cout << num << "00";
or is there a way to multiply with <<??
haha, that's thinking outside the box. Yeah, that'll work for "multiplying" by 100 but if you want to change the multiplier to 5 then you're boned. This made me laugh pretty hard.
ShawnCplus
Code Monkey
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
use the additive property:
(a * m) + (a * n) = a * (m + n)
example:
result = (a << 2) + (a << 4);
// result now contains the sum, 4a + 16a = 20a
cout << a << " times 20 = " << result << endl;
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
er.. i mean "distributive property"
thanks Dave :$
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
are you allowed to just divide by .01
int num;
cin>>num;
int answer / .01; // huh?
the question reads as follows....
show how u can efficiently multiply an integer that is read from the keyboard by 100, without using the operator *. use the << operator several times....
The << operator is required. What happened to the OP anyway?
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
i meant int answer = num/.01;
same thing as multiplying by 100,
but i guess u have to use << so im wrong again
that's not the only reason why you're wrong.
mainly you're wrong because you can't divide an int by a float and expect to get a correct integer result.
go on try it. you might actually learn something.
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
COOOOOOOOOL, dont be dick dude, im a begineer. btw sick avatar, what are you 12?
funny you didnt qualify your "solution" with the "hey i'm a beginner" disclaimer, why are you falling back on it now?
whatever, if you're a beginner, then don't use this site to give wrong advice to other beginners. a good intention is no substitute for correct code.
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
>shu shu shu shut you mouth
You can dish it out, but you can't take it? That's pretty sad.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
never mind. i'm done here. the correct solution has been given back on page 1.
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
For the record, Jephthah, I like your avatar, but I'm having trouble placing it. Is it Stupendousman from Calvin & Hobbes?
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179