Вы находитесь на странице: 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
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
4
5
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
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 0!unt1=1; 0!unt1 <= -; 0!unt1\$\$)
{
f!# (int 0!unt2=1; 0!unt2 <= 5; 0!unt2\$\$)
System!ut"#int (121);
System!ut"#intln();
%
#####
#####
#####
#####
#####
#####
#####
15. f!# (int 0!unt1=1; 0!unt1 <= 5; 0!unt1\$\$)
{
f!# (int 0!unt2=1; 0!unt2 <= 5; 0!unt2\$\$)
System!ut"#int (0!unt1+0!unt2 \$ 1 1);
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
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 loop1
while (,alue <= 0)
{
System!ut"#int (13nte# a "!siti,e ,alue41);
,alue = 5ey6!a#&#ea&Int();
%
1*. .erif/ that the user enters an even value 0use a do loop1
&!
{
System!ut"#int (13nte# an e,en ,alue41);
,alue = 5ey6!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 stored
in a constant called S3N7IN38.
System!ut"#int (13nte# a ,alue41);
,alue = 5ey6!a#&#ea&Int();
while (,alue /= S3N7IN38)
{
System!ut"#intln (,alue);
System!ut"#int (13nte# an!the# ,alue41);
,alue = 5ey6!a#&#ea&Int();
%
1,. 2ead values fro the user, 4uittin! when a sentinel value of - is entered.
5opute and print the product of all values entered 0excludin! the sentinel value1.
"#!&u0t = 1;
System!ut"#int (13nte# a ,alue41);
,alue = 5ey6!a#&#ea&Int();
while (,alue /= 0)
{
"#!&u0t += ,alue;
System!ut"#int (13nte# an!the# ,alue41);
,alue = 5ey6!a#&#ea&Int();
%
System!ut"#intln (19#!&u0t4 1 \$ "#!&u0t);
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 but
not b/ five.
f!# (int 0!unt=8<=; 0!unt <= >I?>; 0!unt\$\$)
if (0!unt*. == 0 @@ 0!unt*5 /= 0)
System!ut"#intln (0!unt);
&'. 6rint all of the factors of a value stored in the variable num6e#. Assue the
value is positive.
f!# (int 0!unt=1; 0!unt <= num6e#; 0!unt\$\$)
if (num6e#*0!unt == 0)
System!ut"#intln (0!unt);
&(. 2ead 1- values fro the user and print the lowest and hi!hest value entered.
System!ut"#int (13nte# a ,alue4 1);
,alue = 5ey6!a#&#ea&Int();
min = maA = ,alue;
f!# (int 0!unt=2; 0!unt <= 10; 0!unt\$\$)
{
System!ut"#int (13nte# an!the# ,alue4 1);
,alue = 5ey6!a#&#ea&Int();
if (,alue < min)
min = ,alue;
if (,alue ) maA)
maA = ,alue;
%
System!ut"#intln (18!west4 1 \$ min);
System!ut"#intln (1>iBhest4 1 \$ maA);
Exercise Worksheet Java Software Solutions
&5. 3eterine and print the nuber of ties the character CaC appears in the
St#inB variable st#.
0!unt = 0;
f!# (int in&eA=0; in&eA < st#lenBth(); in&eA\$\$)
if (st#0ha#At(in&eA) == CaC)
0!unt\$\$;
System!ut"#intln (1Num6e# !f aCs4 1 \$ 0!unt);
&). 6rint the characters stored in the St#inB variable st# backwards.
f!# (int in&eA=st#lenBth()'1; in&eA )= 0; in&eA'')
System!ut"#int (st#0ha#At(in&eA));
&*. 6rint ever/ other character in the St#inB variable st# startin! with the first
character.
f!# (int in&eA=0; in&eA < st#lenBth(); in&eA\$=2)
System!ut"#int (st#0ha#At(in&eA));
&+. 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"a0e=0; s"a0e < line; s"a0e\$\$)
System!ut"#int (1 1);
System!ut"#int (1+1);
%
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 1D!m"ile1, the output would be%
D
!
m
"
i
l
e
f!# (int line=0; line < st#lenBth(); line\$\$)
{
f!# (int s"a0e=0; s"a0e < line; s"a0e\$\$)
System!ut"#int (1 1);
System!ut"#int (st#0ha#At(line));
%