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++;

Answers were Sorted based on User's Feedback



Determine the code below, tell me exactly how many times is the operation sum++ performed ? for ..

Answer / 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

Determine the code below, tell me exactly how many times is the operation sum++ performed ? for ..

Answer / anil

0 times bcoz everytime it enters second loop condition is
not satisfied ,thus comes out of loop.

Is This Answer Correct ?    14 Yes 3 No

Determine the code below, tell me exactly how many times is the operation sum++ performed ? for ..

Answer / daniel

(99 * 100)/2 = 4950
The sum++ is performed 4950 times.

Is This Answer Correct ?    7 Yes 7 No

Determine the code below, tell me exactly how many times is the operation sum++ performed ? for ..

Answer / santosh

when i=0
j=100 and 100>100-1(false) come out of the loop and the sum
is executed 0 times

Is This Answer Correct ?    2 Yes 2 No

Determine the code below, tell me exactly how many times is the operation sum++ performed ? for ..

Answer / lnk

Its quite simple to analyse ...

LOOP i=0: i=0
then enters loop j=o: but it false that always J>100-i;

i.e.., i=0;j=100; 100- i-> 100
so always 100 is not greater than 100

than it comes out no sum++;
Loop i=1; ; j =100 only now 100 - i =99 so j>100 -i
(100>99)
then sum++ is executed ;


i= 1 j=100 j > 100 - i j=99 ;sum ++
i=2 j =99 j> 100 - i j =98 ; sum ++

i=50 j=51 j>100-50 true ( 51>50 ) ; sum++ j=50
i=51 j= 50 j>100-51 true(50>49 ) so no sum++


i = 99 j=2 j>100-i true (2>1) sum ++

so sum++ would be executed 99 times

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More C Interview Questions

Write a program to print "hello world" without using a semicolon?

0 Answers  


What is the difference between functions abs() and fabs()?

0 Answers  


which of the following statements is incorrect a.typedef struct new{ int n1; char n2; } DATA; b.typedef struct { int n3; char *n4; }ICE; c.typedef union { int n5; float n6; } UDT; d.#typedef union { int n7; float n8; } TUDAT;

5 Answers   Assurgent, TCS,


main() { int i; for(i=0;i<5;i++) printf("%d",1l<<i); } why doesn't 'l' affect the code??????

1 Answers  


Explain how do you list files in a directory?

0 Answers  






how to create duplicate link list using C???

0 Answers  


What language is lisp written in?

0 Answers  


define c

6 Answers   HCL, TCS,


a linear linked list such that the link field of its last node points to the first node instead of containing NULL a) linked list b) circular linked list c) sequential linked list d) none

0 Answers  


i have a written test for microland please give me test pattern

0 Answers   Microland,


When do you not use the keyword 'return' when defining a function a) Always b) Never c) When the function returns void d) dfd

0 Answers  


write a program that prints a pascal triangle based on the user input(like how many stages) in an efficient time and optimized code?

3 Answers   Oracle,


Categories