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


Please Help Members By Posting Answers For Below Questions

Which one would you prefer - a macro or a function?

702


What does *p++ do? What does it point to?

705


How to write a multi-statement macro?

711


What does typedef struct mean?

771


How do you determine the length of a string value that was stored in a variable?

742






What does c mean in standard form?

710


What is a MAC Address?

719


How can you read a directory in a C program?

739


Can a void pointer point to a function?

661


What is the use of putchar function?

733


What is strcpy() function?

762


What does %d do?

841


How can you restore a redirected standard stream?

701


What is %g in c?

739


When should structures be passed by values or by references?

677