0

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

7
Contributors
17
Replies
18
Views
8 Years
Discussion Span
Last Post by jephthah
Featured Replies
  • Use the shift operators. [url]http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/[/url] Multiplying by a factor of 2 is much easier than multiplying by 100. Here's a basic example of how to use shift operators. [code] #include <iostream> using namespace std; int main () { int a = 12; // a = 12 cout << a << … Read More

  • 1

    use the additive property: (a * m) + (a * n) = a * (m + n) example: [code=cpp]result = (a << 2) + (a << 4); // result now contains the sum, 4a + 16a = 20a cout << a << " times 20 = " << result << … Read More

  • 1

    er.. i mean "distributive property" thanks Dave :$ Read More

1

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

cout << num << "00";

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

Votes + Comments
thats pretty funny. and not entirely incorrect :)
1

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;
}
Votes + Comments
Nice link !
0

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.

1

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;

.

Votes + Comments
http://en.wikipedia.org/wiki/Distributive
0

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?

0

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

0

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.


.

0

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

-1

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.

Votes + Comments
unbelieveably WRONG.
0

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.

.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.