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

commented: thats pretty funny. and not entirely incorrect :) +9

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;
}
commented: Nice link ! +7

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;

.

commented: http://en.wikipedia.org/wiki/Distributive +19

er.. i mean "distributive property"

thanks Dave :$

commented: Heh :D +7

are you allowed to just divide by .01

int num;
cin>>num;
int answer / .01;

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.

commented: unbelieveably WRONG. -2

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.

.

>shu shu shu shut you mouth
You can dish it out, but you can't take it? That's pretty sad.

never mind. i'm done here. the correct solution has been given back on page 1.


.

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

yes :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.