×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ Operators | Find output programs | Set 2

This section contains the C++ find output programs with their explanations on various operators (set 2).
Submitted by Nidhi, on June 01, 2020

Program 1:

#include <iostream>
using namespace std;

int main()
{
    int A = 5, B = 40, C = 0;

    C = (A << 2) + (B >> 3);
    cout << C;

    return 0;
}

Output:

25

Explanation:

In the above code, we use the left shift and right shift operators. Now we will understand the easy way how it works in the evaluation of an expression in C++?

We can understand the left shift operator using below statement:

    = 10 << 3
    = 10 * pow(2,3)
    = 10 * 8
    = 80

We can understand the right shift operator using below statement:

    = 10 >> 3
    = 10 / pow(2,3)
    = 10 / 8
    = 1

Now we come to the evaluation of expression mentioned in above code.

    C   = (A<<2) + (B>>3)	
        = (5<<2) + (40>>3)
        = 5 * pow(2,2) + 40 /pow(2,3)
        = 5 * 4 + 40 / 8
        = 20 + 5
        = 25

Then the final value of C is 25.

Program 2:

#include <iostream>
using namespace std;

int main()
{
    int C;

    C = (80 >> 5) ? (120 << 3) ? (10 > (5 + 5)) ? 1 : 5 : 10 : 20;
    cout << C;

    return 0;
}

Output:

5

Explanation:

In the above program, we used a nested ternary operator and shift operators, now we evaluate the expression.

    C   =  (80>>5)?(120<<3)?(10>(5+5))?1:5:10:20	
        =  (80/pow(2,5)) ? (120*pow(2,3))?(10>10)?1:5:10:20
        =  (80/32)?(120*8)?(10>10)?1:5:10:20
        =  (2) ? (960)?(10>10)?1:5:10:20
  • 2 it means the condition is true then
  • 960 that is again true
  • Then 10>10 that is false then 5 will be returned.

Then the final value of C is 5.

Program 3:

#include <iostream>
using namespace std;

int main()
{
    int A = 10, B = 20, C = 0;

    C = A++ + ++B * 10 + B++;
    cout << A << "," << B << "," << C;

    return 0;
}

Output:

11, 22, 241

Explanation:

In the above program, we took 3 local variables A, B, and C that contain the values 10, 20, and 0 respectively. Here we used an expression that contains pre, post-increment, and arithmetic operators, as we know that pre-increment increases the value of the variable before evaluation of the expression, and post-increment increases the value of the variable after evaluation.

Values of A and B before the evaluation of expression due to pre-increment are:

    A = 10, B = 21

Now we evaluate the expression:

    C   = 10 + 21 * 10 + 21
        = 10 +210 +21
        = 241

Then the value of C is 241. And values of A and B after evaluation of express due to post-increment operator are:

    A = 11, B = 22

Then the final values are: A =11, B= 22, C=241

Note: Compiler dependency may be there.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.