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."





Of course, it comes to reason that I should test this via C++.  Below is my source code followed by the output.










Comments

Popular posts from this blog

CPPForSchool.com: Variable, Operator & Expression Set 1: Assignment 9

CPPForSchool.com: Array - Single Dimension Set 1 Assignment 6

CPPForSchool.com: Variable, Operator & Expression Set 1: Assignment 8