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
i
is initialized with the value 10. - The
while
loop will run as long asi
is less than 20. - Inside the loop, the modulus operator
%
is used to get the remainder ofi
divided by 2. This operation will return 0 for even numbers and 1 for odd numbers. - The
printf
function is used to print the result of the modulus operation. - The value of
i
is then incremented by 1. - Steps 3 to 5 are repeated until
i
is 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 of the following code?#include <stdio.h>int main() { int i = 0; for (; i < 5; i++); printf("%d ", i); return 0;}
What is the output of the following code?int arr[] = {1, 2, 3, 4, 5};int i;for (i = 0; i < 5; i++) arr[i] = arr[i] + 2;printf("%d\n", arr[3]);
What is the output of the following code snippet?int i = 0;do { if (i == 3) continue; System.out.print(i + " "); i++;} while (i < 5);
What will be the output of the following code? int i = 1;while(i<10){ if (i / 2 == 2) cout << i << “ ”;i++;}
The output of the code will be ____. int i=5; printf(“%d %d %d”, i, i++, ++i); ans.
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.