Results 1 to 8 of 8

Thread: How to write program to generate a pascal triangle in java?

  1. #1
    Join Date
    May 2009
    Posts
    1,193

    How to write program to generate a pascal triangle in java?

    Hi,
    I am new to programming language. I recently started learning java language. Yesterday our sir has given us program like write a program to generate a pascal triangle in java?. I use various method but I unable to write that program. That's why I asked this question on this forum. So if you have any idea about this please help me.Thanks in advanced.

  2. #2
    Join Date
    May 2008
    Posts
    2,302

    re: How to write program to generate a pascal triangle in java?

    Just tried to understand each and every steps.


    Code:
    class PascalTriangle 
    
    { 
    public static void main(String args[]) 
    { 
    int p=6; 
    int triangle[][] = new int[p][p]; 
    for(int q=0;q<p;q++) 
    { 
    for(int r=0;r<p;r++) 
    triangle[q][r]=0; 
    } 
    for(int q=0;q<p;q++) 
    { 
    triangle[q][0]=1 ;
    } 
    
    
    for(int q=1;q<p;q++)
     { 
    for(int r=1;r<p;r++) 
    triangle[q][r]=triangle[q-1][r-1]+triangle[q-1][r]; } 
    
    for(int q=0;q<p;q++) 
    { 
    for(int r=0;r<=q;r++) 
    System.out.print(triangle[q][r]+ " "); 
    System.out.println(); 
    } 
    
    
    
    } 
    }

  3. #3
    Join Date
    Nov 2005
    Posts
    1,323

    re: How to write program to generate a pascal triangle in java?

    Code:
    import java.io.*; 
    public class TriPascal { 
    
    public TriPascal() { 
    } 
    
    public static void main(String[] args) 
    { 
    int step = new Integer(args[0]).intValue(); 
    int col1 = nFilas * 2 - 1 ; 
    int arrP[][] = new int[step][col1]; 
    int cont = 0; 
    
    for(int p=0;p<step;p++){ 
    for(int q=0;q<col1;q++){ 
    arrPascal[p][j]=0; 
    } 
    } 
    
    arrP[0][step-1]=1; 
    for(int p=1;p<step;p++){ 
    for(int q=(step-1)-p;q<col1;q=j+2){ 
    if(cont<=p){ 
    if(q==0||q==col1-1){ 
    arrP[p][q] = 1; 
    }else{ 
    arrP[p][q] = arrP[p-1][q-1] + arrP[p-1][q+1]; 
    } 
    } 
    cont++; 
    } 
    cont = 0; 
    } 
    System.out.println("filas=" + step + "-Columnas=" + col1); 
    for(int p=0;i<step;p++){ 
    for(int q=0;q<col1;q++){ 
    System.out.print(arrP[p][q]); 
    } 
    System.out.println(""); 
    } 
    } 
    
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    1,949

    re: How to write program to generate a pascal triangle in java?

    Here I suggest you logic of your program just tried to understand.



    step1.Make a new array with T number of elements and then insert left most element with value "1" and insert other with value "0"

    step2.Now print all array.

    step3. Now make use of for loop through the array like from N-1 ... I ... 0

    a. Add the I element to the p+1 element. Store the result in the p element

    b. If (p < T-2) goto a. Else, exit the loop

    step4. Goto step 2

  5. #5
    Join Date
    May 2008
    Posts
    2,393

    re: How to write program to generate a pascal triangle in java?

    Code:
    import java.io.;
    import java.util.;
    import gpdraw.;
    import java.lang.;
    public class pascal{
    int[] array1= new int[10];
    public void generate(int input)
    {
    int p=0;
    for(int x=0;x<=input;x++){
    for(int a=0 ;a<=n;a++){
    arra1y[p] = fact(x)/(fact(x-a)fact(a));
    p++;
    }}
    int b=0;
    for (int row = 0; row <=input; row++){
    for(int pos=0; pos<=row; pos++){
    System.out.print(" " array1[d]);
    b+;
    }
    System.out.println();
    
    }}
    public static int fact(int t){
    if( t <= 1 ) 
    return 1;
    else
    return t fact( t - 1 );
    }
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    re: How to write program to generate a pascal triangle in java?

    Code:
    public class Pascal {
        public static int[][] Rows(int p){
        	int[][] ptm = new int[p][];
        	for (int z = 0; z < p; z++) {
                ptm[z] = new int[z + 1];                          
                ptm[z][0] = 1;                          
                for (int q = 1; q< z; q++) {
                  ptm[z][q] = ptm[z - 1][q - 1] + ptm[z - 1][q];  
                }
                ptm[z][z] = 1;                           
              }
              return ptm;
        }
    
     
        public static void main(String[] args) {
            if (args.length != 1) {
              System.out.println("usage: java " + Pascal.class.getName() + " rows");
              System.exit(1);
            }
            int p = Integer.parseInt(args[0]);
            if (p > 0) {
              int[][] pascal1 = Rows(n);
              for (int[] row1 : pascal1) {
        	for (int w : row1) System.out.print(w + " ");
        	System.out.println("");
              }
            }
          }
    }

  7. #7
    Join Date
    Sep 2011
    Posts
    1

    Re: How to write program to generate a pascal triangle in java?

    hey also show output...

  8. #8
    Join Date
    Dec 2007
    Posts
    2,297

    Re: How to write program to generate a pascal triangle in java?

    Quote Originally Posted by Lekha Tikone View Post
    hey also show output...
    I think that the simplest way to do it is probably to generate it line by line. Each line is based on the previous one.
    1. Start with the first line (1)
    2. Add a 1 first in the new line.
    3. Add the first and second number in the previous line and put it on the new line, then the second and third, third and fourth and so on (this will be done zero times on the second row).
    4. Finish by adding a last 1.
    5. Begin a new line if you want one and jump to 2.

Similar Threads

  1. How to write java program to print pyramid of stars?
    By Nadiaa in forum Software Development
    Replies: 6
    Last Post: 19-08-2011, 05:03 PM
  2. Need help to write this program in java?
    By frkadeel in forum Software Development
    Replies: 1
    Last Post: 01-12-2010, 02:58 PM
  3. How to write string data to file using java program?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 08:26 PM
  4. How to write java program to find factorial of number?
    By Balamohan in forum Software Development
    Replies: 5
    Last Post: 28-11-2009, 09:14 PM
  5. how to write program on palindrome in java?
    By Linoo in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 04:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •