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 / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Is c a great language, or what?

708


What is malloc and calloc?

677


What is dynamic dispatch in c++?

651


Is c easy to learn?

649


What are register variables in c?

676






How can I find out if there are characters available for reading?

732


What is #define size in c?

751


What is the heap in c?

738


A SIMPLE PROGRAM OF GRAPHICS AND THEIR OUTPUT I WANT SEE WAHAT OUTOUT OF GRAPHICS PROGRAM

1788


What is the difference between malloc calloc and realloc in c?

748


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

738


What is unsigned int in c?

649


Once I have used freopen, how can I get the original stdout (or stdin) back?

723


How can you increase the size of a statically allocated array?

720


How can I convert a number to a string?

697