CPPForSchool.com: Variable, Operator & Expression Set 2: Assignment 6
6.
What is the output of following program?
int result = 4 + 5 * 6 + 2;
cout << result;
int a = 5 + 7 % 2;
cout << a;
This particular assignment is testing my current understanding of the order of operations, specifically mathematics. From what I understand, I will need to compute the multiplication followed by the addition.
int result = 4 + 5 * 6 + 2;
cout << result;
With that said, 5 * 6 = 30. Then add 4 + 2 + 30, which equates to 36.
int a = 5 + 7 % 2;
cout << a;
This is 7 divided by 2, which equates to 3 with the remainder of 1.
7 % 2 = 1
Followed by
1 + 5 = 6
In order to immediately confirm my answers, there is are buttons within the assignment to "Show the answer."
int result = 4 + 5 * 6 + 2;
cout << result;
int a = 5 + 7 % 2;
cout << a;
This particular assignment is testing my current understanding of the order of operations, specifically mathematics. From what I understand, I will need to compute the multiplication followed by the addition.
int result = 4 + 5 * 6 + 2;
cout << result;
With that said, 5 * 6 = 30. Then add 4 + 2 + 30, which equates to 36.
int a = 5 + 7 % 2;
cout << a;
This is 7 divided by 2, which equates to 3 with the remainder of 1.
7 % 2 = 1
Followed by
1 + 5 = 6
In order to immediately confirm my answers, there is are buttons within the assignment to "Show the answer."
Of course, it comes to reason that I should test this via C++. Below is my source code followed by the output.
Comments
Post a Comment