Spaces:
Running
Running
Create 12a . Counter
Browse files
Week 1: Types, condition clauses and loops/12a . Counter
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import java.util.Scanner;
|
| 2 |
+
|
| 3 |
+
public class Example {
|
| 4 |
+
public static void main(String[] args) {
|
| 5 |
+
Scanner reader = new Scanner(System.in);
|
| 6 |
+
System.out.print("Give a number: ");
|
| 7 |
+
int num = Integer.valueOf(reader.nextLine());
|
| 8 |
+
|
| 9 |
+
int counter = 1;
|
| 10 |
+
while (counter <= num) {
|
| 11 |
+
System.out.println(counter);
|
| 12 |
+
counter++; // increases the value with one
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
Give a number: 5
|
| 19 |
+
1
|
| 20 |
+
2
|
| 21 |
+
3
|
| 22 |
+
4
|
| 23 |
+
5
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
==================================
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
import java.util.Scanner;
|
| 32 |
+
|
| 33 |
+
public class Example {
|
| 34 |
+
public static void main(String[] args) {
|
| 35 |
+
Scanner reader = new Scanner(System.in);
|
| 36 |
+
|
| 37 |
+
while (true) {
|
| 38 |
+
System.out.print("Give a number: ");
|
| 39 |
+
int num = Integer.valueOf(reader.nextLine());
|
| 40 |
+
|
| 41 |
+
if (num == 0) {
|
| 42 |
+
break;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
System.out.println("The square of the number is" + (num * num));
|
| 46 |
+
}
|
| 47 |
+
System.out.println("Hi!");
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
Give a number: 2
|
| 52 |
+
The square of the number is 4
|
| 53 |
+
Give a number: 4
|
| 54 |
+
The square of the number is 16
|
| 55 |
+
Give a number: 6
|
| 56 |
+
The square of the number is 36
|
| 57 |
+
Give a number: 5
|
| 58 |
+
The square of the number is 25
|
| 59 |
+
Give a number: 0
|
| 60 |
+
Hi!
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|