Output of C programs | Set 61 (Loops)

04 Oct, 2017
Prerequisite : Loops in CQ.1 What is the output of this program?
#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;}
Optionsa)A B C D EA B C D EA B C D EA B C D EA B C D E
b)A B C DA B C DA B C DA B C D
c)A B C DA B CA BA
d)A B C D EA B C DA B CA BA
ans:- d 
Explanation :- inner loop iterates for value less than equal to i, thus printingA B C D EA B C DA B CA BA
Q.2 What is the output of this program?
#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;}
Options
a) After loop counter=1b) 1,After loop counter=2c) 1,After loop counter=1d) 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?
#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;}
Optionsa)After loop counter=1b)1,After loop counter=2c)1,After loop counter=1d)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?
#include <iostream>using namespace std;int main(){    int loopvar = 10;    while (printf("Hello ") && loopvar--)        ;    return 0;}
Optionsa)Hellob)Hello Hello Hello Hello .......c)Hello Hello Hello Hello Hello Hello Hello Hello Hello Hellod)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?
#include <iostream>#include <string.h>using namespace std;int main(){    int counter = 1;    while (counter <= 10 && 1 ++)        printf("Hello");    return 0;}
Options
a)compilation errorb)HelloHello ... 10 timesc)HelloHello ... 11 timesd)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.
C-Loops & Control Statements
C-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Output of C++ programs | Set 50
Runtime Errors
C++ Programming Multiple Choice Questions
Output of C Programs | Set 2
Different ways to copy a string in C/C++
Output of C++ Program | Set 1
Output of Python Program | Set 1
Output of Java Program | Set 2
Output of Java Program | Set 3
Output of C++ programs | Set 47 (Pointers)