What is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}
Question
What 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 "0101010101".
Here's the step by step explanation:
- The integer variable
iis initialized with the value 10. - The
whileloop will run as long asiis less than 20. - Inside the loop, the modulus operator
%is used to get the remainder ofidivided by 2. This operation will return 0 for even numbers and 1 for odd numbers. - The
printffunction is used to print the result of the modulus operation. - The value of
iis then incremented by 1. - Steps 3 to 5 are repeated until
iis no longer less than 20.
So, the output will be "0101010101", which represents the alternating pattern of even (0) and odd (1) 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 does the following program print?#include <stdio.h>int main() { for (int i = 1; i < 10; i++) { if (i % 2 == 0) continue; printf("%d ", i); } 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
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 will be the output of the following code?#include <stdio.h>int main() { int i = 0; for (; i < 5; ) { printf("%d ", i); i++; } return 0;}
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.