Q:

C++ Looping | Find output programs | Set 4

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Looping (set 4).

Program 1:

#include <iostream>
using namespace std;

int A = 5;

int fun()
{
    return A--;
}

int main()
{
    int A = 5;

    while (fun()) {
        cout << A + ::A << " ";
    }

    return 0;
}

Program 2:

#include <iostream>
using namespace std;

int A = 5;

int& fun()
{
    return A--;
}

int main()
{
    int A = 5;

    while (fun()) {
        cout << A + ::A << " ";
    }
    return 0;
}

Program 3:

#include <iostream>
#define MACRO(VAL) (char*)(&VAL + 1) - (char*)(&VAL)

using namespace std;

int main()
{
    int i = 1;
    int num = 0;

    num = MACRO(num);

    while (i <= num) {
        cout << "India ";
        i++;
    }
    return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

9 8 7 6 5

Explanation:

In the above program, we defined a function in which we are accessing the value of the global variable A and decreases it using the post-decrement operator.

In the main() function, we also declared a local variable A. Both local and global variables contain the same value that is 5.

Now look to the while loop, here we used the return value of fun() as a condition in the loop, as we know that condition is true till the returned value is non-zero.

Note: fun() can't access the local variable A while in the main() global variable A is accessing using the Scope resolution operator (::).

Interation1: 
The function fun() returns 5, condition is true, and global 'A' become 4. Then
		cout<<A+::A<<" ";
Here the above statement will print 5+4 that is 9. 

Interation2: 
The function fun() returns 4, condition is true, and global 'A' become 3. Then
		cout<<A+::A<<" ";
Here the above statement will print 5+3 that is 8. 

Interation3: 
The function fun() returns 3, condition is true, and global 'A' become 2. Then
		cout<<A+::A<<" ";
Here the above statement will print 5+2 that is 7. 

Interation4: 
The function fun() returns 2, condition is true, and global 'A' become 1. Then
		cout<<A+::A<<" ";
Here the above statement will print 5+1 that is 6. 

Interation5: 
The function fun() returns 1, condition is true, and global 'A' become 0. Then
		cout<<A+::A<<" ";
Here the above statement will print 5+0 that is 5. 

Interation6: 
The function fun() returns 0, and then condition will be false.

Then the final output will be "9 8 7 6 5".

Answer Program 2:

Output:

main.cpp:8:13: error: invalid initialization of non-const 
reference of type ‘int&’ from an rvalue of type ‘int’
     return A--;
            ~^~

Explanation:

Here, we defined a function that returning the reference.

In C++, A function that returns reference will be used as an LVALUE, but in the above program, we are using fun() as an RVALUE.

Answer Program 3:

Output:

India India India India

Explanation:

In the above program, we defined a macro MACRO that will calculate the size in bytes of a specified variable just like sizeof() operator, here we calculate the displacement of variable, subtract the current address by next address, then we got different that denotes the size of the variable.

Here we used a 32-bit system, then MACRO will return 4 because here we passed an integer variable to it.

num = MACRO(num);

Then the value of num will be 4. Then the loop will execute 4 times and print "India " 4 times.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Looping | Find output programs | Set 5... >>
<< C++ Looping | Find output programs | Set 3...