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 < 20){ printf("%d", 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 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;}

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.