Wednesday, June 26, 2013

C++ (beginner) mathematical operators

+

i think everybody knows what + does.
-
the same for -

*
This is the multiplying operator

/
This is the division operator

=
This operator is used to give a variable a value.
example

int b;
b=10;
we used the = operator to give b the value of 10.

Special cases with / operator

10 is not divisible on 3 but if you write this you will get exactly 3 when 10 /3 =3.3333333333(3)
When we divide integer on integer we will always get an integer. The computer will simply do the division and will throw away the decimal part. If you divide float on float you will get float. What to do if we want to divide an integer on integer but to  get a float number.
just write
cout<<double( a) /double(b);
double(a) will see the variable a as a float number and as we already know float number divided on float number is float number.

% operator

the % operator is the modulo operator.
a%b will return us the remainder of a divided on b.




IF  YOU HAVE ANY QUESTIONS , LEAVE THEM IN COMMENTS, NO QUESTION WILL STAY UNANSWERED.

1 comment:

  1. Hehehe THis is a great article for beginners!! I love your blog!

    ReplyDelete