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
Explain enumerated types.
What is modeling?
Explain what are binary trees?
What is 2c dna?
What is a list in c?
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.
What is the best way to comment out a section of code that contains comments?
How can you find out how much memory is available?
Is there anything like an ifdef for typedefs?
What is character constants?
How many levels of indirection in pointers can you have in a single declaration?
What is d scanf?
What is conio h in c?
What is the usage of the pointer in c?
What is the difference between NULL and NUL?