Question 1 Operator Precedence a What is the value of varia
Question 1 : Operator Precedence
a) What is the value of variable n after the following C++ statements are executed?
int n;
n = 8+3.5*(int)4.5/2;
b) What is the value of variable n after the following C++ statements are executed?
int a = 15, b = 2, c = 7;
int n;
n = a/b*(c%2 + 4.5);
Solution
Solution1.cpp
#include <iostream>//header file for input output function
using namespace std;//it tells the compiler to link stdnamespace
int main()
{//main function
int n;
n = 8+3.5*(int)4.5/2;
cout<<\"value of n is \"<<n;
return 0;
}
output
value of n is 15
Solution2.cpp
#include <iostream>//header file for input output function
using namespace std;//it tells the compiler to link stdnamespace
int main()
{//main function
int a = 15, b = 2, c = 7;//variable assignment
int n;//variable declaration
n = a/b*(c%2 + 4.5);
cout<<\"value of n is \"<<n<<endl;
return 0;
}
output
value of n is 38
