Saltar al contenido

ejemplo de código de código java del triángulo de pascal

Ejemplo 1: programa Java para mostrar el triángulo pascal

import java.util.Scanner;publicclass PascalsTriangleJava 

   static int findFactorial(int number)
   
      int factorial;
      for(factorial = 1; number > 1; number--)
      
         factorial *= number;
      
      return factorial;
   
   // here's the function to display pascal's trianglestatic int printPascalTraingle(int num, int p)
      return findFactorial(num) / (findFactorial(num - p) * findFactorial(p));
   publicstatic void main(String[] args)
      int row, a, b;
      System.out.println("Please enter number of rows: ");
      Scanner sc = new Scanner(System.in);
      row = sc.nextInt();
      System.out.println("Here's is pascal's triangle: ");
      for(a = 0; a < row; a++) 
      
         for(b = 0; b < row - a; b++)
         
            System.out.print(" ");
         for(b =0; b <= a; b++)
            System.out.print(" " + printPascalTraingle(a, b));
         
         System.out.println();
      
      sc.close();
   

Ejemplo 2: triángulo de pascales java

/*
Author: Jeffrey Huang
*/
import java.util.*;publicclass PascalTriangleCreator

    public static long factorial(long n)
        /*
        The whole purpose of this method is to find the factorial of a number,
        since java does not have a built in method for it. Calculating n choose 
        r is done using factorial, and since this code will be used repeatedly,
        it is wise to put it in a separate method.
        */
        long factorial;
        if (n==0)
            factorial=1;
        else
            factorial=1;
            for (int counter=1;counter<=n;counter++)
                factorial=factorial*counter;
            
        
        return factorial;
    
    
    publicstatic long FinalValue(long n, long r)
        //Calculates n choose r by calling the factorial method.
        return factorial(n) / ( factorial(n-r) * factorial(r) );
    publicstatic void main(String[] args)
     Scanner sc=new Scanner (System.in);
     long rows=1;
     long i,j;
     while (rows!=0)rows>20)
      System.out.println("Invalid input.");
      System.out.println("How many rows of Pascal's triangle would you like to print? (0 to stop; 1-20 rows)");
      rows=sc.nextLong();
  /*
  The following ifelse block makes the code more efficient.Otherwise,if the user 
  enters zero at any other point than at the start of the loop, the program will go 
  through the long process of trying to print a triangle before terminating the
  program. 
  
  Using the following method, it istrue that rows==0is tested for twice, but
  it shortens the execution time immensely. And we know that when zero istruefor the if statement, it is guaranteed to be true when breaking the loop.*/if(rows==0)
      System.out.println("Program terminated by user.");
  else
  for(i = 0; i < rows; i++) 
      //Iterates through the number of rows required.
         for(j = 0; j <= rows-i; j++)
           System.out.print("   ");
            //Iterates the printing of spaces.
         for(j =0; j <= i; j++)
           if ((FinalValue(i, j))>9999) 
             System.out.print(" "); 
           elseif((FinalValue(i, j))>999)
             System.out.print("  "); 
           elseif((FinalValue(i, j))>99)
             System.out.print("   "); 
           elseif((FinalValue(i, j))>9)
             System.out.print("    "); 
           else
            System.out.print("     "); 
           
            System.out.print(FinalValue(i, j));//Prints a number of spaces plus a number.
         
         System.out.println();
        
        
     
 sc.close();
 

Si te ha resultado de utilidad este artículo, sería de mucha ayuda si lo compartes con más seniors así nos ayudas a difundir este contenido.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *