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

Explain enumerated types.

792


What is modeling?

815


Explain what are binary trees?

820


What is 2c dna?

819


What is a list in c?

787


Input is "rama loves rajesh and rajesh Loves rama also and rajesh wear gloves and bloves" To print output is count the numbers of times repeted the word love without case sensitive.

1833


What is the best way to comment out a section of code that contains comments?

1050


How can you find out how much memory is available?

801


Is there anything like an ifdef for typedefs?

883


What is character constants?

890


How many levels of indirection in pointers can you have in a single declaration?

816


What is d scanf?

780


What is conio h in c?

804


What is the usage of the pointer in c?

814


What is the difference between NULL and NUL?

995