Write the output generated by the following code fragments 1

Write the output generated by the following code fragments:

--------#1----------

int n = 128;

int j = 0;

while( (2 * j) < n )

{

j++;

}

System.out.println ( \"j: \" + j);//outside the loop

--------#2-----------

for(int j = -5; j < 9; j = j + 3 )

System.out.print( j + \" \" ) ;

--------#3---------------

int answer = 0;

int n = 5;

for(int loopCounter = 1; loopCounter <= n; loopCounter++ )

answer += loopCounter;

System.out.println ( \"Answer is: \" + answer );//outside the loop

Solution

#1

value of j depends on how many times loops runs.

lets suppose

x=2*j , here j is increasing by 1

values of x: 0,2,4,6,8,10,......126 (because x < 128)

we have to calculate number of terms in this series=j

126=0+(j-1)*2

2*j=128

j=64

Output: j:64

--------------------------------------------------------------------

#2

j is increasing by 3 so

Output: -5 -2 1 4 7

---------------------------------------------------------------

#3

answer = answer+loopCounter

answer=0+1=1

answer=1+2=3;

answer=3+3=6;

answer=6+4=10

answer=10+5=15

Output: Answer is 15

Write the output generated by the following code fragments: --------#1---------- int n = 128; int j = 0; while( (2 * j) < n ) { j++; } System.out.println ( \
Write the output generated by the following code fragments: --------#1---------- int n = 128; int j = 0; while( (2 * j) < n ) { j++; } System.out.println ( \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site