Examveda
Examveda

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

A. 1

B. 2

C. 9

D. 0.5

E. 0.25

Answer: Option B


This Question Belongs to C Program >> Control Structures

Join The Discussion

Comments ( 4 )

  1. Akshita Panchal
    Akshita Panchal :
    4 years ago

    the program ran successfully and terminated. because there is not any printing statement for anything.

  2. Mrityunjoy Sengupta
    Mrityunjoy Sengupta :
    6 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.

  3. Hareem Ahmad
    Hareem Ahmad :
    6 years ago

    how

  4. Pawan Pawar
    Pawan Pawar :
    7 years ago

    give explaination of this program

Related Questions on Control Structures