• Lenguaje

    Java

  • Descripción

    Programa que pide el número de niveles para mostrar el Triángulo de Pascal

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
import java.util.Scanner;

public class Pascal {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int i, j, n;
        System.out.print("Ingrese el n\243mero de niveles: ");
        n = in.nextInt();
        int[] buffer = new int[n+2];
        int[] fila   = new int[n+2];
        for (j=0; j<n+2; j++) {
            buffer[j] = 0;
            fila  [j] = 0;
        }
        for (i=0; i<=n; i++) {
            fila[0] = 1;
            fila[i] = 1;
            for (j=1; j<=i+1; j++)
                fila[j] = buffer[j-1] + buffer[j];
            for (j=n-1; j>=i; j--)
                System.out.print("   ");
            for (j=0; j<=i; j++)
                System.out.printf("%5d ", fila[j]);
            System.out.println();
            for (j=0; j<n+2; j++)
                buffer[j] = fila[j];
        }
    }

}