0% found this document useful (0 votes)
8 views21 pages

5 - Loop

Uploaded by

Central Hùng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views21 pages

5 - Loop

Uploaded by

Central Hùng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Loop

QUYENNV
While Loop
While Loop
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
// Create a Scanner
Scanner input = new Scanner(System.in);
System.out.print("What is " + number1 + " + " + number2 + "? ");
int answer = input.nextInt();
while (number1 + number2 != answer) {
System.out.print("Wrong answer. Try again. What is “ + number1 + " + " +
number2 + "? ");
answer = input.nextInt();
}
System.out.println("You got it!");
Case study: Guess number
Case study: Guess number II
Case study: Multiple
Subtraction Quiz
Do-while loop
The do-while loop executes the loop body
first, then checks the loopcontinuation-
condition to determine whether to
continue or terminate the loop.
Loop terminates. The difference between a
while loop and a do-while loop is the order
in which the loop-continuation-condition
is evaluated and the loop body executed.
You can write a loop using either the while
loop or the do-while loop. Sometimes one
is a more convenient choice than the other.
For loop
For loop
Nested loop
Case study: Dec to Hex
Hexadecimals are often used in computer systems programming (see Appendix F
for an introduction to number systems). How do you convert a decimal number to a
hexadecimal number? To convert a decimal number d to a hexadecimal number is
to find the hexadecimal digits hn, hn - 1, hn - 2, c , h2, h1, and h0 such that
d = hn * 16^n + h(n – 1) * 16^(n – 1) + h(n – 2) * 16^(n – 2) + … + h2 * 16^2 + h1 *
16^1 + h0 * 16^0
These hexadecimal digits can be found by successively dividing d by 16 until the
quotient is 0. The remainders are h0, h1, h2, c , hn - 2, hn - 1, and hn. The
hexadecimal digits include the decimal digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, plus A,
which is the decimal value 10; B, which is the decimal value 11; C, which is 12; D,
which is 13; E, which is 14; and F, which is 15.
For example, the decimal number 123 is 7B in hexadecimal. The conversion is
done as follows. Divide 123 by 16. The remainder is 11 (B in hexadecimal) and the
quotient is 7. Continue divide 7 by 16. The remainder is 7 and the quotient is 0.
Therefore 7B is the hexadecimal number for 123.
Case study: Dec to Hex
Case study: Palindrome
A string is a palindrome if it reads the same forward
and backward. The words “mom,” “dad,” and “noon,”
for instance, are all palindromes.
The problem is to write a program that prompts the
user to enter a string and reports whether the string is
a palindrome. One solution is to check whether the first
character in the string is the same as the last character.
If so, check whether the second character is the same
as the second-to-last character. This process continues
until a mismatch is found or all the characters in the
string are checked, except for the middle character if
the string has an odd number of characters.
Exercise I
Exercise II
Exercise III
Exercise IV
Exercise V
Exercise VI - VIII
VI. (Perfect number) A positive integer is called a perfect number if it is
equal to the sum of all of its positive divisors, excluding itself. For
example, 6 is the first perfect number because 6 = 3 + 2 + 1. The next
is 28 = 14 + 7 + 4 + 2 + 1. There are four perfect numbers less than
10,000. Write a program to find all these four numbers.

VII. (Display prime numbers between 2 and 1,000) A program to display


all the prime numbers between 2 and 1,000, inclusive. Display eight
prime numbers per line. Numbers are separated by exactly one space.

VIII. (Reverse a string) Write a program that prompts the user to enter a
string and
displays the string in reverse order. (do not use any built-in function)
For example,
Enter a string: ABCD (Enter)
The reversed string is DCBA
Exercise IX
Exercise X

You might also like