Flowchart Practice Worksheet

Draw the flowchart for each problem, then write the Java code. Swap papers with a classmate and check each other using the answer key at the end.

Name: ______________________________   Score: ______ / 11

Part A: Draw the flowchart

1 · Sequence

Input two numbers and print their sum.

2 · if-else

Input a whole number. Print "Even" if it is even, otherwise "Odd".

3 · if-else-if

Input an age. Print "Minor" if below 18, "Adult" if 18 to 59, and "Senior" if 60 or above.

4 · while

Print the numbers 10 down to 1 using a while loop.

5 · for

Compute and print the sum of the even numbers from 2 to 20 using a for loop.

6 · do-while

Keep asking for a password until the user types java123, then print "Welcome!".

7 · sentinel

Input numbers until -1 is entered. Print how many numbers were entered (don't count the -1).

8 · selection

Input three numbers and print the largest.

Part B: Trace the code (write the output)

9
int x = 3;
while (x <= 12) {
    System.out.print(x + " ");
    x = x + 4;
}

Output: ______________________

10
int total = 0;
for (int i = 5; i >= 1; i--) {
    if (i == 3) continue;
    total += i;
}
System.out.println(total);

Output: ______________________

11
int k = 100;
do {
    k = k / 2;
} while (k > 10);
System.out.println(k);

Output: ______________________

Answer Key

Flowcharts are written as text: (Start) terminal, /.../ input/output, [...] process, <...?> decision. Different but correct flowcharts are fine, as long as the logic is the same.

1 · Sum of two numbers

(Start) → /Input a, b/ → [sum = a + b] → /Print sum/ → (End)
int a = sc.nextInt();
int b = sc.nextInt();
int sum = a + b;
System.out.println(sum);

2 · Even or odd

(Start) → /Input n/ → <n % 2 == 0?> True → /Print "Even"/ ─┐ False → /Print "Odd"/ ─┴→ (End)
int n = sc.nextInt();
if (n % 2 == 0) System.out.println("Even");
else            System.out.println("Odd");

3 · Age group

(Start) → /Input age/ → <age < 18?> True → /Print "Minor"/ ──────────────────┐ False → <age < 60?> │ True → /Print "Adult"/ ────────┤ False → /Print "Senior"/ ───────┴→ (End)
if (age < 18)      System.out.println("Minor");
else if (age < 60) System.out.println("Adult");
else               System.out.println("Senior");

4 · Count down 10 to 1

(Start) → [i = 10] → <i >= 1?> ◄──────────┐ True → /Print i/ → [i = i - 1] ┘ False → (End)
int i = 10;
while (i >= 1) {
    System.out.println(i);
    i--;
}

5 · Sum of evens 2 to 20 (answer: 110)

(Start) → [sum = 0, i = 2] → <i <= 20?> ◄───────────────┐ True → [sum = sum + i] → [i = i + 2] ┘ False → /Print sum/ → (End)
int sum = 0;
for (int i = 2; i <= 20; i += 2) {
    sum += i;
}
System.out.println(sum);   // 110

6 · Password (do-while)

(Start) → /Input pw/ ◄──────────────────────┐ → <pw is not "java123"?> True ─────┘ False → /Print "Welcome!"/ → (End)
String pw;
do {
    pw = sc.nextLine();
} while (!pw.equals("java123"));
System.out.println("Welcome!");

7 · Count until -1 (sentinel)

(Start) → [count = 0] → /Input num/ → <num != -1?> ◄────────┐ True → [count++] → /Input num/ ┘ False → /Print count/ → (End)
int count = 0;
int num = sc.nextInt();
while (num != -1) {
    count++;
    num = sc.nextInt();
}
System.out.println(count);

8 · Largest of three

(Start) → /Input a, b, c/ → [max = a] → <b > max?> True → [max = b] ─┐ False ─────────────┴→ <c > max?> True → [max = c] ─┐ False ─────────────┴→ /Print max/ → (End)
int max = a;
if (b > max) max = b;
if (c > max) max = c;
System.out.println(max);

Part B answers

9. 3 7 11. x goes 3 → 7 → 11 → 15, and 15 <= 12 is false.

10. 12. 5 + 4 + 2 + 1 = 12 (3 is skipped by continue).

11. 6. k goes 100 → 50 → 25 → 12 → 6 (integer division). 6 > 10 is false.