Output of C programs | Set 61 (Loops)

Prerequisite : Loops in C
Q.1 What is the output of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
int main()
{
    int i, j, var = 'A';
  
    for (i = 5; i >= 1; i--) {
        for (j = 0; j < i; j++)
            printf("%c ", (var + j));
        printf("\n");
    }
    return 0;
}

chevron_right


Options
a)A B C D E
A B C D E
A B C D E
A B C D E
A B C D E

b)A B C D
A B C D
A B C D
A B C D

c)A B C D
A B C
A B
A

d)A B C D E
A B C D
A B C
A B
A

ans:- d 

Explanation :- inner loop iterates for value less than equal to i, thus printing
A B C D E
A B C D
A B C
A B
A

Q.2 What is the output of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
int main()
{
    int counter = 1;
    do {
        printf("%d, ", counter);
        counter += 1;
    } while (counter >= 10);
    printf("\nAfter loop counter=%d", counter);
    printf("\n");
    return 0;
}

chevron_right


Options

a) After loop counter=1
b) 1,
After loop counter=2
c) 1,
After loop counter=1
d) After loop counter=2

ans:- b

Explanation :- do while is an exit controlled loop, here loop body executed first, then condition will be checked.

Q.3 What is the output of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
int main()
{
    int counter = 1;
    while (counter >= 10) {
        printf("%d, ", counter);
        counter += 1;
    }
    printf("\nAfter loop counter=%d", counter);
    printf("\n");
    return 0;
}

chevron_right


Options
a)After loop counter=1
b)1,
After loop counter=2
c)1,
After loop counter=1
d)After loop counter=2

ans:- a

Explanation :- since while is an entry controlled loop so, here condition will be checked first.

Q.4 What is the output of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
int main()
{
    int loopvar = 10;
    while (printf("Hello ") && loopvar--)
        ;
    return 0;
}

chevron_right


Options
a)Hello
b)Hello Hello Hello Hello …….
c)Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
d)Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello

ans:- d

Explanation :- since post decrement operator is used so value is decremented after expression loopvar – – is evaluated. Thus, Hello is printed 11 times.

Q.5 What is the output of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int counter = 1;
    while (counter <= 10 && 1 ++)
        printf("Hello");
    return 0;
}

chevron_right


Options

a)compilation error
b)HelloHello … 10 times
c)HelloHello … 11 times
d)Hello

ans:- a

Explanation :- Error: lvalue required as increment operand. It is a compile time error.

This article is contributed by Pragya Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up