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 variable
iis initialized with the value 1. - The
whileloop will run as long asiis less than 10. - Inside the loop, there is an
ifcondition that checks ifidivided by 2 equals 2. This condition will only be true wheniequals 4. - When
iequals 4, the program will print the value ofi(which is 4) followed by a space. - After printing, the value of
iis incremented by 1. - The loop continues to run, but since the
ifcondition is not met for any other value ofi, no other values are printed. - Once
iequals 10, thewhileloop condition is no longer met, and the loop ends. - Therefore, the only output of this code is "4".
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++; }}
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.