Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

I'm having difficulty finding out how many executions these loops operate sum++. i'm not only interested in the answer but how youy figured it out.

A) for(i=1 ; i<= 100 ; i++)

for (j= 1 ; j <= i; j++) sum++;

A) i'm thinking that the first for loop goes through 100 times and the second for loop is also 100 times so 100^2 is correct?? is this right?

B) for(i = 101; i <=200; i++)

for(j= i - 1; j <i+1;j++) sum++;

B) I tried walking through it and it seems the first loop starts out and goes through 99 times. hen thew second one is true for 99 times so 99^2

my thinking was as follows

1st time through j =100 ; 100 < 101 +1; 100++

2nd time j=101 ; 101 <102+1; 101++

.

.

.

.

199th time j= 199 ; 199 < 200 +1 ; 200++ so stops here 199-100 = 99 times....is this the right logic??

A) for(i=1 ; i<= 100 ; i++)

for (j= 1 ; j <= i; j++) sum++;

A) i'm thinking that the first for loop goes through 100 times and the second for loop is also 100 times so 100^2 is correct?? is this right?

B) for(i = 101; i <=200; i++)

for(j= i - 1; j <i+1;j++) sum++;

B) I tried walking through it and it seems the first loop starts out and goes through 99 times. hen thew second one is true for 99 times so 99^2

my thinking was as follows

1st time through j =100 ; 100 < 101 +1; 100++

2nd time j=101 ; 101 <102+1; 101++

.

.

.

.

199th time j= 199 ; 199 < 200 +1 ; 200++ so stops here 199-100 = 99 times....is this the right logic??

Thanks in advance!

That is correct, I overlooked the <=.

> how did you know to take (i+1) - (i-1)

just the difference between the upper and lower ranges for the index.

1) for(int i = 100 ; i > 0 ;i--)

for(j=i; j < 2 * i + 2 ; j++)

sum++

1. i'm thinking the first loop goes through 99 times but then when you get into the second loop it lookslike this

i=100 i=99 .................... i=1

======= ======== =========

j=100 j=99 j=1

i=202 i=20 i=4 so its 3+4+5+6..........+102 = times sum++ is executred is this correct?

102 times 101 times 3 times

2) for(int i = 0 ; i <10000 ;i++)

for(j=0 ; j *j*j < i ; j++)

sum++

this one i have no clue can someone walk me through it ?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

first time (i=1) it will execute once, second time (i=2) it will execute twice, etc

So total loops = 1 + 2 + 3 + ... + 100.

B) No, again right for the first loop, but not for the second.

The second loop will execute (i+1) - (i-1) times = 2 times

So total number of loops is 99 x 2 = 198 times.