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 standard functions are available to manipulate strings?
How does #define work?
What is volatile variable in c?
Can you define which header file to include at compile time?
What is uint8 in c?
How can I list all of the predefined identifiers?
What are the types of data files?
What are the types of pointers in c?
What is the use of printf() and scanf() functions?
What is c language & why it is used?
What are the advantages of using new operator as compared to the function malloc ()?
exit () is used to a) exit () terminates the execution of the program itself b) exit () terminates the execution of the loop c) exit () terminates the execution of the block d) none of the above
Why c is a procedural language?
What is the scope of an external variable in c?
What is the use of static variable in c?