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