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
Input two numbers and print their sum.
Input a whole number. Print "Even" if it is even, otherwise "Odd".
Input an age. Print "Minor" if below 18, "Adult" if 18 to 59, and "Senior" if 60 or above.
Print the numbers 10 down to 1 using a while loop.
Compute and print the sum of the even numbers from 2 to 20 using a for loop.
Keep asking for a password until the user types java123, then print "Welcome!".
Input numbers until -1 is entered. Print how many numbers were entered (don't count the -1).
Input three numbers and print the largest.
int x = 3;
while (x <= 12) {
System.out.print(x + " ");
x = x + 4;
}
Output: ______________________
int total = 0;
for (int i = 5; i >= 1; i--) {
if (i == 3) continue;
total += i;
}
System.out.println(total);
Output: ______________________
int k = 100;
do {
k = k / 2;
} while (k > 10);
System.out.println(k);
Output: ______________________
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.
int a = sc.nextInt(); int b = sc.nextInt(); int sum = a + b; System.out.println(sum);
int n = sc.nextInt();
if (n % 2 == 0) System.out.println("Even");
else System.out.println("Odd");
if (age < 18) System.out.println("Minor");
else if (age < 60) System.out.println("Adult");
else System.out.println("Senior");
int i = 10;
while (i >= 1) {
System.out.println(i);
i--;
}
int sum = 0;
for (int i = 2; i <= 20; i += 2) {
sum += i;
}
System.out.println(sum); // 110
String pw;
do {
pw = sc.nextLine();
} while (!pw.equals("java123"));
System.out.println("Welcome!");
int count = 0;
int num = sc.nextInt();
while (num != -1) {
count++;
num = sc.nextInt();
}
System.out.println(count);
int max = a; if (b > max) max = b; if (c > max) max = c; System.out.println(max);
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.