**C++** a. Write a program containing the following expressions. x should be an int variable. Add a statement after each one to print out the current value of x in the following format (of course, you will print the value of x, not the constant 4!): Problem 1. x has the value 4
1. x = 4;
2. x = 2;
3. x = 24 – 6 * 2; (show each computation)
4. x = -15 * 2 + 3; (show each computation)
5. x = 72 / 5; (why not 14.4?)
6. x = 72 % 5;
7. x = 5 * 2 / 6 + 15 % 4; (show each computation) On this paper write the answer printed and a small note for each problem indicating the value given and why the given value was computed. Refer to the actions of operators and precedence of operations in your answers when appropriate. b. Add the following statements to your program and once again print the value of x for each one:
8. x = x + 1;
9. x++;
10. x = x + 3;
11. x += 3;
12. x += x + 3;
13. x--; Write a small note for each problem above indicating the answer and why the given values were printed.
c. Add the following statements to your program (where z is an int variable). Print the value of z for each numbered problem.
14. z = x * x;
15. z = z % 10;
16. z = x * x; z = z / 10;
17. z = x * x; z = (z / 10) % 10; Write a small note for each problem indicating the answer and why the given values were printed.
d. Finally, declare a double variable y. Once again, add the following statements and print the value of y after each one.
18. y = 72 / 5;
19. y = 72 / 5.0; Write a small note for each problem indicating why the given values were printed.
a.
#include <iostream>
using namespace std;
int main()
{
int x;
x = 4;
cout << "x has value " << x <<endl;
x = 2;
cout << "x has value " << x <<endl;
x = 24 - 6 * 2; // x=24-(6*2)=24-12=12
cout << "x has value " << x <<endl;
x = -15 * 2 + 3; //(-15*2)+3=-30+3=-27
cout << "x has value " << x <<endl;
x = 72 / 5; //because it is of int type it prints quotient of 75
divides 5
cout << "x has value " << x <<endl;
x = 72 % 5; //it prints the remainder of 75 divided by 5
cout << "x has value " << x <<endl;
x = 5 * 2 / 6 + 15 % 4;
//((5*2)/6)+(15%4)
//(10/6)+3
//1+3
//4
cout << "x has value " << x <<endl;
}
Output:
After executing the statements in 'a' the value stores in x is 4.
now we will execute the statements in 'b' by using the x value as 4.
b.
#include <iostream>
using namespace std;
int main()
{
int x;
x = 4;
cout << "x has value " << x <<endl;
x = x+1; // x increments by 1
cout << "x has value " << x <<endl;
x++; //post increment
//x has been incremented after the execution of this
statement
//if we done k=x++; the k is equal to previous value of x
//x is incremented by 1
cout << "x has value " << x <<endl;
x = x + 3; // x increments by 3
cout << "x has value " << x <<endl;
x += 3; //this statement is equal to (x=x+3)
cout << "x has value " << x <<endl;
x += x + 3;//this statement is equal to x=x+(x+3)
//x=12+(12+3)
//x=12+15=27
cout << "x has value " << x <<endl;
x--; //x decrements by 1
cout << "x has value " << x <<endl;
}
Output:
After executing the statements in 'b' the value stores in x is 26.
now we will execute the statements in 'c' by using the x value as 26.
c.
#include <iostream>
using namespace std;
int main()
{
int x,z;
x = 26;
cout << "x has value " << x <<endl;
z=x*x; //z=26*26=676
cout << "z has value " << z <<endl;
z=z%10; //z=676%10 = last digit of number 67'6'=6.
cout << "z has value " << z <<endl;
cout << "x has value " << x <<endl;
z=x*x; //z=676
cout << "z has value " << z <<endl;
z=z/10; //z=676/10 = all digits of number except last one '67'6 =
67
cout << "z has value " << z <<endl;
cout << "x has value " << x <<endl;
z=x*x; //z= 676
cout << "z has value " << z <<endl;
z=(z/10)%10; //z=(676/10)%10=67%10=7
cout << "z has value " << z <<endl;
}
d.
#include <iostream>
using namespace std;
int main()
{
double y;
y = 72/5;
//Here 72,5 both are int data type
//it calculates the value of 72/5 that is
//quotient of 72 divides 5 = 14
cout << "y has value " << y <<endl;
y = 72/5.0;
//But here 72 is int and 5.0 is double
//so fractional calculation happens here
// and the value is 14.4.
cout << "y has value " << y <<endl;
}
If you have any queries, please comment below.
Please upvote , if you like this answer.
Get Answers For Free
Most questions answered within 1 hours.