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....

ok i can do this using overload.... or thats the only way i can think of... or is it actually possible to use << to muliply the number.


my code so far... (just to get the number)

#include <iostream>
#include <stdlib.h>

using namespace std;

int num;

int main(int argc, char *argv[])
{
  cout << "Enter a number from: ";
  cin >> num;
     
  system("PAUSE");	
  return 0;
}

oh and thanx

im not thinking at all.... all i can do is say

cout << num << "00";

or is there a way to multiply with <<??

Comments
thats pretty funny. and not entirely incorrect :)

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.

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;

.

Comments
http://en.wikipedia.org/wiki/Distributive

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?

i meant int answer = num/.01;
same thing as multiplying by 100,
but i guess u have to use << so im wrong again

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.


.

@jephthan
>> go on try it. you might actually learn something.

COOOOOOOOOL, dont be dick dude, im a begineer.
btw sick avatar, what are you 12?

If you want to multiply any number by 100
Try this :-

int result = 0;
   int x;
   cout << "Enter Any Number " << endl;
  cin>>x;
  for ( int i = 1 ; i <= (100 /2) ; i++)
 {
		result += x << 1;
		
}

if u want to multiply by n just replace 100 with n.
But BEWARE this works when n is even only.
Find by yourself how to overcome this when n is odd.

Comments
unbelieveably WRONG.

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.

.

For the record, Jephthah, I like your avatar, but I'm having trouble placing it. Is it Stupendousman from Calvin & Hobbes?

This article has been dead for over six months. Start a new discussion instead.