ExamVeda
Login
Home
This question belongs to C Program Control Structures
Control Structures
?

What will be the value of sum after the following program is executed?
void main()
{
   int sum=1, index = 9;
   do{
      index = index – 1;
      sum *= 2;
   }while( index > 9 );
}

Answer & Solution
Correct Answer: Option B
Let's discuss the solution. Join the discussion
Examveda
Question posted by Examveda
Community

Join the Discussion

4 Comments
Akshita Panchal
Akshita Panchal 6 years ago
the program ran successfully and terminated. because there is not any printing statement for anything.
Mrityunjoy Sengupta
Mrityunjoy Sengupta 8 years ago
It is property of do-while loop to get executed at least once irrespective of condition.
here the value of index is initially 9.
when this goes int do-while loop it gets decremented by 1 as index=index-1;
then sum *=2 is executed which can be expanded as sum=sum*2;
so sum becomes 2;
when condition is checked index=8 so 8>9 becomes false,
thus finally value of sum becomes 2.
Hareem Ahmad
Hareem Ahmad 9 years ago
how
Pawan Pawar
Pawan Pawar 9 years ago
give explaination of this program