7
Exercise Worksheet Java Software Solutions Loops (with solutions) For exercises 1 to 15, indicate the output that will be produced. Assue the followin! declarations are ade "ust before each exercise. #hat is, assue these initiali$ations are in effect at the be!innin! of each proble% final int MIN = 10, MAX = 20; int num = 15; 1. while (num < MAX) 15 { 16  System!ut"#intln (num); 17  num = num $ 1; 18 % 19 &. while (num < MAX) 16 { 17  num = num $ 1; 18  System!ut"#intln (num); 19 % 20 '. &! 16 { 17  num = num $ 1; 18  System!ut"#intln (num); 19 % 20 while (num <= MAX); 21 (. while (num < MAX) 15 { 14  System!ut"#intln (num); 13  num = num ' 1;  infinitely % 5. while (num MIN) 15 { 14  System!ut"#intln (num); 13  num = num ' 1; 12 % 11

Loops Solutions

Embed Size (px)

Citation preview

8/12/2019 Loops Solutions

http://slidepdf.com/reader/full/loops-solutions 1/7

Exercise Worksheet Java Software Solutions

Loops (with solutions)

For exercises 1 to 15, indicate the output that will be produced. Assue the followin!

declarations are ade "ust before each exercise. #hat is, assue these initiali$ations are

in effect at the be!innin! of each proble%

final int MIN = 10, MAX = 20;

int num = 15;

1. while (num < MAX) 15

{ 16

  System!ut"#intln (num); 17

  num = num $ 1; 18

% 19

&. while (num < MAX) 16

{ 17

  num = num $ 1; 18

  System!ut"#intln (num); 19

% 20

'. &! 16

{ 17

  num = num $ 1; 18

  System!ut"#intln (num); 19

% 20

while (num <= MAX); 21

(. while (num < MAX) 15

{ 14

  System!ut"#intln (num); 13

  num = num ' 1;   infinitely%

5. while (num MIN) 15{ 14

  System!ut"#intln (num); 13

  num = num ' 1; 12

% 11

8/12/2019 Loops Solutions

http://slidepdf.com/reader/full/loops-solutions 2/7

Exercise Worksheet Java Software Solutions

). while (num < MAX) 15

{ 17

  System!ut"#intln (num); 19

  num $= 2;

%

*. while (num < MAX) 16

{ 18

  if (num*2 == 0)

  System!ut"#intln (num);

  num$$;

%

+. &! 21

{

  num = num $ 1;

  if (num+2 MAX$num)

  System!ut"#intln (num);

%

while (num <= MAX);

. f!# (int alue=0; alue = -; alue$$) 0

  System!ut"#intln (alue); 1

2

3

45

6

7

1-. f!# (int alue=-; alue < 0; alue'') 7

  System!ut"#intln (alue); 6

5

4

3

2

1

11. f!# (int alue=1; alue <= 20; alue$=.) 1

  System!ut"#intln (alue); 5

9

13

17

8/12/2019 Loops Solutions

http://slidepdf.com/reader/full/loops-solutions 3/7

Exercise Worksheet Java Software Solutions

1&. f!# (int alue=num; alue <= MAX; alue$$) 15

  System!ut"#intln (alue); 16

17

18

19

20

1'. f!# (int alue=num; alue <= MAX; alue$$) 15

  if (alue*. /= 0) 17

  System!ut"#intln (alue); 18

19

1(. f!# (int !unt1=1; !unt1 <= -; !unt1$$)

{

  f!# (int !unt2=1; !unt2 <= 5; !unt2$$)

  System!ut"#int ();

  System!ut"#intln();%

#####

#####

#####

#####

#####

#####

#####

15. f!# (int !unt1=1; !unt1 <= 5; !unt1$$)

{

  f!# (int !unt2=1; !unt2 <= 5; !unt2$$)

  System!ut"#int (!unt1+!unt2 $ );

  System!ut"#intln();

%

1 2 3 4 5

2 4 6 8 10

3 6 9 12 15

4 8 12 16 20

5 10 15 20 25

8/12/2019 Loops Solutions

http://slidepdf.com/reader/full/loops-solutions 4/7

Exercise Worksheet Java Software Solutions

For exercises 1) to &, write code se!ents that will perfor the specified action.

1). erif/ that the user enters a positive value. 0use a while loop

while (alue <= 0){

  System!ut"#int (3nte# a "!sitie alue4);

  alue = ey6!a#&#ea&Int();

%

1*. erif/ that the user enters an even value 0use a do loop

&!

{

  System!ut"#int (3nte# an een alue4);

  alue = ey6!a#&#ea&Int();%

while (alue*2 /= 0);

1+. 2ead and print values entered b/ a user until a particular sentinel value is

encountered. 3o not print the sentinel value. Assue the sentinel value is storedin a constant called S3N7IN38.

System!ut"#int (3nte# a alue4);

alue = ey6!a#&#ea&Int();

while (alue /= S3N7IN38)

{  System!ut"#intln (alue);

  System!ut"#int (3nte# an!the# alue4);

  alue = ey6!a#&#ea&Int();

%

1. 2ead values fro the user, 4uittin! when a sentinel value of - is entered.

opute and print the product of all values entered 0excludin! the sentinel value.

"#!&ut = 1;

System!ut"#int (3nte# a alue4);

alue = ey6!a#&#ea&Int();while (alue /= 0)

{

  "#!&ut += alue;

  System!ut"#int (3nte# an!the# alue4);

  alue = ey6!a#&#ea&Int();

%

System!ut"#intln (9#!&ut4 $ "#!&ut);

8/12/2019 Loops Solutions

http://slidepdf.com/reader/full/loops-solutions 5/7

Exercise Worksheet Java Software Solutions

&-. 6rint the odd nubers between 1 and 1--.

f!# (int num=1; num <= ::; num$=2)

  System!ut"#intln (num);

&1. 6rint the ultiples of ' fro '-- down to '.

f!# (int num=00; num = ; num'=)

  System!ut"#intln (num);

&&. 6rint the nubers between 78W and 9:;9 that are evenl/ divisible b/ four butnot b/ five.

f!# (int !unt=8; !unt <= >I?>; !unt$$)

  if (!unt*. == 0 @@ !unt*5 /= 0)

  System!ut"#intln (!unt);

&'. 6rint all of the factors of a value stored in the variable num6e#. Assue the

value is positive.

f!# (int !unt=1; !unt <= num6e#; !unt$$)

  if (num6e#*!unt == 0)

  System!ut"#intln (!unt);

&(. 2ead 1- values fro the user and print the lowest and hi!hest value entered.

System!ut"#int (3nte# a alue4 );

alue = ey6!a#&#ea&Int();min = ma = alue;

f!# (int !unt=2; !unt <= 10; !unt$$)

{

  System!ut"#int (3nte# an!the# alue4 );

  alue = ey6!a#&#ea&Int();

  if (alue < min)

  min = alue;

  if (alue ma)

  ma = alue;

%

System!ut"#intln (8!west4 $ min);System!ut"#intln (>iBhest4 $ ma);

8/12/2019 Loops Solutions

http://slidepdf.com/reader/full/loops-solutions 6/7

Exercise Worksheet Java Software Solutions

&5. 3eterine and print the nuber of ties the character CaC appears in the

St#inB variable st#.

!unt = 0;

f!# (int in&e=0; in&e < st#lenBth(); in&e$$)  if (st#ha#At(in&e) == CaC)

  !unt$$;

System!ut"#intln (Num6e# !f aCs4 $ !unt);

&). 6rint the characters stored in the St#inB variable st# backwards.

f!# (int in&e=st#lenBth()'1; in&e = 0; in&e'')

  System!ut"#int (st#ha#At(in&e));

&*. 6rint ever/ other character in the St#inB variable st# startin! with the first

character.

f!# (int in&e=0; in&e < st#lenBth(); in&e$=2)

  System!ut"#int (st#ha#At(in&e));

&+. 6rint a se4uence of asterisk characters in the followin! confi!uration, continuin!

for 7:<ES nuber of asterisks.

+

 +

  +

  +

  +

  +

  +

f!# (int line=0; line < 8IN3S; line$$)

{

  f!# (int s"ae=0; s"ae < line; s"ae$$)

  System!ut"#int ( );

  System!ut"#int (+);

%

8/12/2019 Loops Solutions

http://slidepdf.com/reader/full/loops-solutions 7/7

Exercise Worksheet Java Software Solutions

&. 6rint the characters of a St#inB variable st# in a dia!onal line downward. For

exaple, if st# contained D!m"ile, the output would be%

D

 !  m

  "

  i

  l

  e

f!# (int line=0; line < st#lenBth(); line$$)

{

  f!# (int s"ae=0; s"ae < line; s"ae$$)

  System!ut"#int ( );

  System!ut"#int (st#ha#At(line));%