Spaces:
Running
Running
| 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 | |