| | |
multiply using << several times
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 29
Reputation:
Solved Threads: 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)
oh and thanx
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)
C++ Syntax (Toggle Plain Text)
#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
Last edited by phoenix911; May 26th, 2009 at 10:25 am. Reason: 4got to say thanx :/
•
•
Join Date: May 2009
Posts: 29
Reputation:
Solved Threads: 0
im not thinking at all.... all i can do is say
or is there a way to multiply with <<??
C++ Syntax (Toggle Plain Text)
cout << num << "00";
or is there a way to multiply with <<??
Last edited by phoenix911; May 26th, 2009 at 10:27 am. Reason: was a bit slow
•
•
Join Date: Jan 2008
Posts: 3,817
Reputation:
Solved Threads: 501
Use the shift operators.
http://www.learncpp.com/cpp-tutorial...ise-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.
http://www.learncpp.com/cpp-tutorial...ise-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.
C++ Syntax (Toggle Plain Text)
#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; }
•
•
•
•
im not thinking at all.... all i can do is say
C++ Syntax (Toggle Plain Text)
cout << num << "00";
or is there a way to multiply with <<??
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
use the additive property:
(a * m) + (a * n) = a * (m + n)
example:
.
(a * m) + (a * n) = a * (m + n)
example:
cpp Syntax (Toggle Plain Text)
result = (a << 2) + (a << 4); // result now contains the sum, 4a + 16a = 20a cout << a << " times 20 = " << result << endl;
.
Last edited by jephthah; May 26th, 2009 at 3:42 pm.
•
•
Join Date: May 2009
Posts: 8
Reputation:
Solved Threads: 0
are you allowed to just divide by .01
C++ Syntax (Toggle Plain Text)
int num; cin>>num; int answer / .01;
•
•
Join Date: Jan 2008
Posts: 3,817
Reputation:
Solved Threads: 501
•
•
•
•
are you allowed to just divide by .01
int num; cin>>num; int answer / .01; // huh?
•
•
•
•
i meant int answer = num/.01;
same thing as multiplying by 100,
but i guess u have to use << so im wrong again
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.
.
Last edited by jephthah; May 28th, 2009 at 11:43 am.
![]() |
Similar Threads
- Square root program without sqrt or pwr (C++)
- How do I? Edit Crontab using a Script? (Shell Scripting)
- Hex Addition Help (C++)
- need help here... (Assembly)
- cstdio input? (C++)
- Need help with Data Structure and Algorithms (C)
- After Instal GNU/Linux Fedora Core 6 show HZ? (IT Professionals' Lounge)
- Motorola T720 Phone and Verizon Wireless (Cellphones, PDAs and Handheld Devices)
- opcode and instructions in bits (C++)
- Help ME (C++)
Other Threads in the C++ Forum
- Previous Thread: help me on this tutorial
- Next Thread: Expression Evaluator
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






