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 );
}
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
Join The Discussion
Comments ( 4 )
Related Questions on Control Structures
Which control structure is used to repeatedly execute a block of code in C?
A. for loop
B. if statement
C. switch case
D. while loop
In C, what is the purpose of the 'break' statement within a loop?
A. Continue to the next iteration
B. Exit the program
C. Terminate the loop and exit it
D. Skip the current iteration
What is the purpose of the 'else' statement in C's 'if-else' control structure?
A. Execute the 'if' block
B. Execute the 'else' block
C. Execute both 'if' and 'else' blocks
D. Skip the 'if' block
Which control structure is used to make a decision between two or more alternatives in C?
A. switch case
B. for loop
C. if statement
D. while loop
the program ran successfully and terminated. because there is not any printing statement for anything.
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.
how
give explaination of this program