CWhat is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}
Question
CWhat is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}
Solution
The output of the given code will be a sequence of 0s and 1s. This is because the code is printing the remainder when i is divided by 2 (i.e., i % 2), which is a way to determine if a number is even (remainder 0) or odd (remainder 1).
Here is the step-by-step breakdown:
- The variable
iis initialized to 10. - The
whileloop will run as long asiis less than 20. - Inside the loop, the remainder of
idivided by 2 is printed. Ifiis even, this will be 0. Ifiis odd, this will be 1. iis then incremented by 1.- Steps 3 and 4 are repeated until
iis no longer less than 20.
So, the output will be:
0 1 0 1 0 1 0 1 0 1
This represents the even/odd status of the numbers from 10 to 19.
Similar Questions
What is the output for the following code?12345678910111213#include <stdio.h> int main() { int i = 1; while (i <= 10) { if (i % 5 == 0) { i++; continue; } printf("%d ", i); i++; } return 0; }
What would be the output of the following program? #include <stdio.h> int main( ) { int j=1; while (j <= 255) { printf ( "%c %d\n ", j, j ); j++; } return 0; }
What Is The Output Of The Following Code?#IncludeVoid Main(){ Int S=0; While(S++<10) { If(S<4 && S<9) Continue; Printf("\N%D\T",S); }}
Consider the following code. What is the output?#include <stdio.h>int main() { int x = 5, y = 0; for (int i = 0; i < 5; i++) { y += x; if (y > 10) break; } printf("%d\n", y); return 0;}
What is the output of the code given below?#include <stdio.h> void main() { int i; for (i = 5; i < 10; i++); printf("%d ", i);}Options5105 6 7 8 99
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.