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
What is chain pointer in c?
why we wont use '&' sing in aceesing the string using scanf
write a c program to find the largest and 2nd largest numbers from the given n numbers without using arrays
What does d mean?
What is the size of enum in c?
What is an array? What the different types of arrays in c?
what is the structure pointer?
Explain can static variables be declared in a header file?
What are the types of macro formats?
Why we use int main and void main?
What is realloc in c?
Explain how can a program be made to print the name of a source file where an error occurs?
Why is c not oop?
How can you determine the size of an allocated portion of memory?
What is mean by Data Driven framework in QTP? Can any one answer me in details on this regard.