Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class NumberPyramid {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num = Integer.parseInt(scanner.nextLine()),
- counter = 1;
- for (int row = 1; row <= num; row++) {
- for (int col = 0; col < row; col++) {
- if (counter > num) {
- break;
- }
- System.out.printf("%d ", counter);
- counter++;
- }
- if (counter > num) {
- break;
- }
- System.out.println();
- }
- }
- }
- ИЛИ:
- import java.util.Scanner;
- public class NumberPyramid {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num = Integer.parseInt(scanner.nextLine()),
- counter = 1;
- for (int row = 1; row <= num || counter <= num; row++) {
- for (int col = 0; col < row && counter <= num; col++) {
- System.out.printf("%d ", counter++);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement