Determine the code below, tell me exactly how many times is
the operation sum++ performed ?
for ( i = 0; i < 100; i++ )
for ( j = 100; j > 100 - i; j--)
sum++;
Answer Posted / abdur rab
for ( i = 0; i < 100; i++ )
for ( j = 100; j > 100 - i; j--)
sum++;
first iteration i = 0
j = 100
j loop is executed untill ( 100 - i ) (ie 100 - 0 = 100 )
so output is 0 ( sum is incremented 0 times )
second iteration i = 1
j = 100
j loop is executed untill ( 100 - i ) (ie 100 - 1 = 99 )
so output is 1 ( sum is incremented 1 times )
third iteration i = 2
j = 100
j loop is executed untill ( 100 - i ) (ie 100 - 2 = 98 )
so output is 1 ( sum is incremented 2 times )
0 + 1 + 2 + 3.......+ 99 = ( n (n+1) ) / 2
( 99 (99+1) ) / 2 = 4950
Is This Answer Correct ? | 15 Yes | 1 No |
Post New Answer View All Answers
Which one would you prefer - a macro or a function?
What does *p++ do? What does it point to?
How to write a multi-statement macro?
What does typedef struct mean?
How do you determine the length of a string value that was stored in a variable?
What does c mean in standard form?
What is a MAC Address?
How can you read a directory in a C program?
Can a void pointer point to a function?
What is the use of putchar function?
What is strcpy() function?
What does %d do?
How can you restore a redirected standard stream?
What is %g in c?
When should structures be passed by values or by references?