Spaces:
Running
Running
Create 16B. The squares of the numbers
Browse files
Week 1: Types, condition clauses and loops/16B. The squares of the numbers
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
From the given lines of code, form a program that calculates and prints the squares of even numbers.
|
| 2 |
+
Your task is to correct the faulty solution above.
|
| 3 |
+
The task requires that the lines of code are also correctly indented!
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
int num = 0;
|
| 8 |
+
while (num <= 30) {
|
| 9 |
+
num++;
|
| 10 |
+
|
| 11 |
+
// for odd numbers, exit the IF statement and go back to the top of the while loop, to get the next 'num'
|
| 12 |
+
if (num % 2 == 1) {
|
| 13 |
+
continue;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
// for even numbers, calculate the square and print it
|
| 17 |
+
int square = num * num;
|
| 18 |
+
System.out.println("The square of the number is: " + square);
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|