Spaces:
Running
Running
Create 17. Number triangle
Browse files
Week 1: Types, condition clauses and loops/17. Number triangle
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write a program that asks the user to enter an integer.
|
| 2 |
+
The program will then print a number triangle corresponding to the integer number according to the example printouts shown below.
|
| 3 |
+
Example output:
|
| 4 |
+
Give a number: 4
|
| 5 |
+
1
|
| 6 |
+
12
|
| 7 |
+
123
|
| 8 |
+
1234
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
import java.util.Random;
|
| 13 |
+
import java.util.Scanner;
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
public class Test{
|
| 18 |
+
public static void main(String[] args){
|
| 19 |
+
final Random r = new Random();
|
| 20 |
+
|
| 21 |
+
Scanner reader= new Scanner(System.in);
|
| 22 |
+
System.out.print("Give a number: ");
|
| 23 |
+
int user_num = Integer.valueOf(reader.nextLine());
|
| 24 |
+
|
| 25 |
+
for (int row=1; row<=user_num; row++) {
|
| 26 |
+
|
| 27 |
+
String rowprintout = "";
|
| 28 |
+
for (int col=1; col<=row; col++) {
|
| 29 |
+
rowprintout = rowprintout + col;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
System.out.println(rowprintout);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
Testing with input 4
|
| 43 |
+
Give a number: 4
|
| 44 |
+
1
|
| 45 |
+
12
|
| 46 |
+
123
|
| 47 |
+
1234
|
| 48 |
+
|
| 49 |
+
Testing with input 6
|
| 50 |
+
Give a number: 6
|
| 51 |
+
1
|
| 52 |
+
12
|
| 53 |
+
123
|
| 54 |
+
1234
|
| 55 |
+
12345
|
| 56 |
+
123456
|
| 57 |
+
|
| 58 |
+
Testing with input 7
|
| 59 |
+
Give a number: 7
|
| 60 |
+
1
|
| 61 |
+
12
|
| 62 |
+
123
|
| 63 |
+
1234
|
| 64 |
+
12345
|
| 65 |
+
123456
|
| 66 |
+
1234567
|
| 67 |
+
|
| 68 |
+
Testing with input 5
|
| 69 |
+
Give a number: 5
|
| 70 |
+
1
|
| 71 |
+
12
|
| 72 |
+
123
|
| 73 |
+
1234
|
| 74 |
+
12345
|
| 75 |
+
|
| 76 |
+
Testing with input 1
|
| 77 |
+
Give a number: 1
|
| 78 |
+
1
|
| 79 |
+
|
| 80 |
+
|