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 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.