0
What is the output of this programme and why?????plz get understood me...
#include<iostream> using namespace std; int main() { int a=3,b=8; cout<<a+b/a; return 0; }
4 Respostas
+ 6
This programme will output a value of 5.
Reason :
In arithmetic operators, division comes first , followed by multiplication, addition followed by subtraction.
So the equation that you want to print out is : "a+b/a"
In this case, 'a' is 3, and 'b' is 8 , so fill in the algebra with numbers first.
This becomes "3+8/3". Now remember that division comes first, so we need to divide 8 by 3 first, which would result in 2.67(3.sf). But remember that both 'b' and 'a' are 'int', so you can't have a decimal value, so you need to take the integer value of the result from '2.67', which is 2.
Next, this will make the equation become '3+2' , which just becomes 5, and the system will print out the value '5'
(For the division part, if ANY one of the two variable you are using to divide off each other is a decimal or can hold decimal values, then the result produced can hold decimal values.)
+ 3
Output is 5.
because first 8 divide through 3 is 2.
and then a + 2 is 5.
+ 1
@Ajeet Singh:
Here's the math 3 + 8 / 3
Order of operations tell us we have to divide 8 by 3 then add this result to 3.
What you have to keep in mind is the data type used in this snippet is int (integer). c++ is automatically truncating (cutting off) any of the number that that is not a whole number So 8 / 3 = 2.67 but in c++ anything after decimal point is removed because the int type is being used.
3 + (8 / 3)
= 3 + 2
= 5
0
Thanks to all