Advertisement
MadCortez

Untitled

Sep 30th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. import java.util.Scanner;
  2. class MyClass {
  3.     public static void main(String[] args){
  4.         final int MIN_SIZE = 0;
  5.         final int MAX_SIZE = 21;
  6.         boolean isNotValid = true;
  7.         int n;
  8.         int temp;
  9.         System.out.println("Данная программа строит n строк треугольника Паскаля");
  10.         System.out.print("Введите число n в диапазоне " + (MIN_SIZE + 1) + ".." + (MAX_SIZE - 1)
  11.         + ": ");
  12.         Scanner in = new Scanner(System.in);
  13.         do {
  14.             n = Integer.parseInt(in.nextLine());
  15.             if (n > MIN_SIZE && n < MAX_SIZE)
  16.                 isNotValid = false;
  17.             else
  18.                 System.out.println("Введите число n в заданном диапазоне");
  19.         } while (isNotValid);
  20.         int a[][] = new int[n][n];
  21.         System.out.println(n + " строк треугольника Паскаля: ");
  22.         for (int i = 0; i < n; i++) {
  23.             a[i][0] = 1;
  24.             a[i][i] = 1;
  25.             for (int j = 1; j < i; j++)
  26.                 a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
  27.             for (int j = 0; j <= i; j++)
  28.                 System.out.print(a[i][j] + " ");
  29.             System.out.println();
  30.         }
  31.     }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement