Java >> Tutoriel Java >  >> Java

Programme Java pour générer un triangle Pascal

Dans ce tutoriel, nous allons apprendre à générer un triangle de Pascal dans un tableau 1D. Mais avant d'aller plus loin, si vous n'êtes pas familier avec les concepts du tableau, alors consultez l'article Tableaux en Java. Par exemple,

Saisie : Nombre de lignes :5

Sortie :

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

Programme 1 :Générer le triangle de Pascal

Dans cette approche, nous verrons comment générer le Triangle de Pascal à l'aide d'un tableau.

Algorithme

  1. Démarrer
  2. Déclarez une variable pour le nombre de lignes.
  3. Demandez à l'utilisateur d'initialiser le nombre de lignes.
  4. Déclarez un tableau 1D.
  5. Utilisez trois boucles for pour générer le triangle pascal.
  6. Utilisez la première boucle for externe pour parcourir toutes les lignes.
  7. Utilisez la deuxième boucle for pour imprimer l'espace.
  8. Attribuez le premier élément de chaque ligne à 1.
  9. Utilisez la troisième boucle for pour imprimer les éléments.
  10. Afficher le triangle Pascal.
  11. Arrêter

Vous trouverez ci-dessous le code correspondant.

Le programme ci-dessous montre comment générer un triangle de Pascal.

/*JAVA PROGRAM TO GENERATE PASCAL TRIANGLE IN 1D ARRAY */
import java.util.*;

public class PascalTriangle
{
     public static void main(String []args)
     {
         Scanner sc=new Scanner(System.in);   //Take input from the user
         int i, j, k, l, r;            //Declarig Variabless 
          int a[]=new int[30];     //Declare a 1d array
         
         System.out.println("Enter the number of rows ");
         r=sc.nextInt();      //Initialize the number of rows
    
         //For Pascal Triangle
         for(i=0;i<r;i++)   //Iterate through all the rows
		 {
			for(k=r; k>i; k--)    //Print the number of spaces
			{
				System.out.print(" ");
			}
            a[i] = 1;   //Initialize the first element of each row as 1
			for(j=0;j<=i;j++)    //To find the Pascal triangle element
			{
				 System.out.print(a[i]+ " ");    //Print the array elements
                 a[i] = a[i] * (i - j) / (j + 1);   //Store the pascal triangle elements in an array
			}
			System.out.println();   //To move to the next line
		 }
        
     }
}


Entrez le nombre de lignes 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Programme 2 :Générer le triangle de Pascal

Dans cette approche, nous verrons comment générer le triangle de Pascal à l'aide de deux tableaux.

Algorithme

  1. Démarrer
  2. Déclarez une variable pour le nombre de lignes.
  3. Demandez à l'utilisateur d'initialiser le nombre de lignes.
  4. Déclarez deux tableaux.
  5. Imprimez 1 pour le premier élément de la première ligne.
  6. Initialiser le premier élément des deux tableaux comme 1.
  7. Utilisez quatre boucles for pour la même chose.
  8. Utilisez la première boucle for pour parcourir toutes les lignes.
  9. Utilisez la deuxième boucle for pour imprimer l'espace.
  10. Utilisez la troisième boucle for pour initialiser les nombres.
  11. Utilisez la quatrième boucle for pour imprimer les nombres.
  12. Afficher le résultat final.
  13. Arrêter

Vous trouverez ci-dessous le code correspondant.

/*JAVA PROGRAM TO GENERATE PASCAL TRIANGLE IN 1D ARRAY */
import java.util.*;

public class PascalTriangle
{
     public static void main(String []args)
     {
         Scanner sc=new Scanner(System.in);     //Take input from the user
         int i, j, k, l;            //Declarig Variabless 
          int array[]=new int[30];     //using 1d array
          int temp[]=new int[30];       //using 1d array
          
         int num;    //Declaring variable for the number of rows
         System.out.println("Enter the number of rows ");
         num=sc.nextInt();      //Initialize the number of rows
         
         temp[0] = 1;     //Initializing first variable of the array as 1
         array[0] = 1;   //Initializing first variable of the array as 1
    
    System.out.println("1");     //For first element
    for (i = 1; i < num; i++)      //To iterate through all the rows 
    {
        for (j = 0; j < i; j++)    //To print the space
        System.out.print("");
        for (k = 1; k < num; k++)
        {
            array[k] = temp[k - 1] + temp[k];      //Initialize the array to store the pascal triangle elements
        }
        array[i] = 1;
        for (l = 0; l <= i; l++)
        {
            System.out.print(array[l]+" ");  //Print the array elements
            temp[l] = array[l];    //Copy the array elements to another array
        }
        System.out.println("");    //For next line
    }
        
     }
}


Entrez le nombre de lignes 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1


Balise Java