Simple math problem

Pages: 12
I'm having trouble with this simple math problem and I feel really stupid...


How come 1 * 10 + 2 * 10 + 3 is 123 and not 33? :/
It is 33...
Nope, it's 123... and i figured out why.

1 * 10 = 10
10 + 2 = 12
12 * 10 = 120
120 + 3 = 123

:)
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    int i = 1 * 10 + 2 * 10 + 3;
    std::cout << i;
    
    std::cin.get();
    return 0;
}


This code gives the output 33.

Due to the order of precedence that operators have, and the * being higer than +, the code actually executes as (1 * 10) + (2 * 20) + 3;
Last edited on
It's 33 in my compiler but 123 on my calculator.

Is the order of precedence in math and c++ different?
Thats a very good question, but unfortunately one I can't answer :( would like to know the answer though... *hint* *hint* Helios, Duoas, Grey Wolf, Bazzy, Zaita etc lol...
The order of operations in math is:

Parentheses
Exponents
Multiplication / Division
Addition / Subtraction

You problem is:

1 * 10 + 2 * 10 + 3

which equals 10+20+3

which equals 30.

You calculator must be screwed up.
There are two types of calculator:

One's used by accountants (non mathemetical ones) and the like which will give the answer as 123 - then they are
proper calculators which will give the answer as 33.

If you are a mathematician, or any sort of technical person or engineer - you buy the one
that gives 33 as the answer (because in your job you will be doing proper maths).

So in your case Kuzco throw away your calculator - it is no good for a computer programmer.
Last edited on
I used the windows calculator, I guess it is wrong since both google and wolfram aplha says it is 33 and not 123 :)
The mathematical operators in C++ are the same as those in maths ( I would think all
programming languages behaved this way)
The windows calculator has two modes - Scientific and Standard (look under the View menu option.

Scientific mode gives the answer 33.
Standard mode gives 123.
Ok, thanks :)

Why would the standard one give 123 when it is wrong? :/
I honestly can't provide a definitive answer.

Some people/professions are mathematically oriented and understand operator precedence
and it is important for what they do. These use the 33 calculator.

Some people (99% of the population) or professions are mathematically clueless - so they use the 123 calculator.
Here's a quick precedence test: 1+2*3. If the result is 7, there's operator precedence. If the result is 9, there isn't.
lmao kuzko ....

First, I think you need the () bars in c++... I think it would know precedence--what comes first,multiplication.

therefore:
How come 1 * 10 + 2 * 10 + 3 is 123 and not 33? :/

int c = (1*10)+(2*10)+3;
kuzko

lol how old are you?

you haven't got a scientific calculator yet?

and if you're young and trying to learn c++, right on!
I am 18 years old, how is that relevant?

And I knew it was 33, but the calculator in windows said it was 123. And I don't think I'm good enough in math to argue with a calculator :)
............................................________
....................................,.-‘”...................``~.,
.............................,.-”...................................“-.,
.........................,/...............................................”:,
.....................,?......................................................\,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:”........./
..............?.....__.........................................:`.........../
............./__.(.....“~-,_..............................,:`........../
.........../(_....”~,_........“~,_....................,:`........_/
..........{.._$;_......”=,_.......“-,_.......,.-~-,},.~”;/....}
...........((.....*~_.......”=-._......“;,,./`..../”............../
...,,,___.\`~,......“~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-”
............/.`~,......`-...............................\....../\
.............\`~.*-,.....................................|,./.....\,__
,,_..........}.>-._\...................................|..............`=~-,
.....`=~-,_\_......`\,.................................\
...................`=~-,,.\,...............................\
................................`:,,...........................`\..............__
.....................................`=-,...................,%`>--==``
........................................_\..........._,-%.......`\
...................................,<`.._|_,-&``................`\

In windows calculator standard mode you can only evaluate one expression at a time. Now please burn this thread and never come back.
Last edited on
Why facepalm? How was I supposed to know that it only can evaluate one expression at a time?
Pages: 12