Knowee
Questions
Features
Study Tools

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 &lt; 20) {
    printf(&quot;%d&quot;, i % 2);
    i++;
}
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the given code will be "0101010101".

Here's the step by step explanation:

  1. The integer variable i is initialized with the value 10.
  2. The while loop will run as long as i is less than 20.
  3. Inside the loop, the modulus operator % is used to get the remainder of i divided by 2. This operation will return 0 for even numbers and 1 for odd numbers.
  4. The printf function is used to print the result of the modulus operation.
  5. The value of i is then incremented by 1.
  6. 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.

This problem has been solved

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.

1/3

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.