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++;
}
Solution
The output of the given code will be "4". Here's the step by step explanation:
- The code starts with an integer
i
initialized to 1. - Then it enters a while loop which will continue to execute as long as
i
is less than 10. - Inside the loop, there's an if condition that checks if
i
divided by 2 equals 2. If it does, it prints the value ofi
followed by a space. - After the if condition,
i
is incremented by 1. - 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.
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;}
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.