?
What will be the value of sum after the following program is executed?
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 );
}
Join the Discussion
Login to post a comment or share your explanation.
Loginhere 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.