Knowee
Questions
Features
Study Tools

What will be the output of the following code?          int i = 1;while(i<10){  if (i / 2 == 2)     cout << i << “ ”;i++;}

Question

What will be the output of the following code?          int i = 1;while(i<10){  if (i / 2 == 2)     cout << i << “ ”;i++;}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the given code will be "4". Here's the step by step explanation:

  1. The variable i is initialized with the value 1.
  2. The while loop will run as long as i is less than 10.
  3. Inside the loop, there is an if condition that checks if i divided by 2 equals 2. This condition will only be true when i equals 4.
  4. When i equals 4, the program will print the value of i (which is 4) followed by a space.
  5. After printing, the value of i is incremented by 1.
  6. The loop continues to run, but since the if condition is not met for any other value of i, no other values are printed.
  7. Once i equals 10, the while loop condition is no longer met, and the loop ends.
  8. Therefore, the only output of this code is "4".

This problem has been solved

Similar Questions

What will be the output of the following code?int num = 2;int count=0;while(num!= 0){if ( num & 01)count ++;num >>=1;}cout<< count;return 0;

What will be the result?1. int i = 10;2. while(i++ <= 10){3. i++;4. }5. System.out.print(i);10111213

CWhat is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}

What is the output of the following code snippet?int i = 0;while (i < 5) {    if (i == 3)        break;    System.out.print(i + " ");    i++;}

What is the output of the programme below?public static void main(String[] args) {     int x = 0;     while (x < 10) {          if (x & 2 == 0) {               System.out.print(x + “ “);          }          x++;     }}

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.