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&lt;10){  
    if (i / 2 == 2)  
        cout &lt;&lt; i &lt;&lt; &quot; &quot;;  
    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 code starts with an integer i initialized to 1.
  2. Then it enters a while loop which will continue to execute as long as i is less than 10.
  3. Inside the loop, there's an if condition that checks if i divided by 2 equals 2. If it does, it prints the value of i followed by a space.
  4. After the if condition, i is incremented by 1.
  5. The loop then continues to the next iteration.

So, the only value of i for which i / 2 equals 2 is i = 4. Therefore, "4" will be printed.

This problem has been solved

Similar Questions

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

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

How does a WHILE loop start?{$a->questionintifier} Yanıta.while (i <= 10; i++)b.while (i <= 10)c.while i = 1 to 10d.while (i=0; i <= 10; i++)

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);

. Find the output of the following program.main(){ int i = (1, 2, 3); cout << i << endl;}

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.