Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 27-11-2009
Member
 
Join Date: May 2009
Posts: 1,180
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.

Reply With Quote
  #2  
Old 27-11-2009
Member
 
Join Date: May 2008
Posts: 2,283
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(); 
} 



} 
}
Reply With Quote
  #3  
Old 27-11-2009
Member
 
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(""); 
} 
} 

}
Reply With Quote
  #4  
Old 27-11-2009
Member
 
Join Date: Apr 2008
Posts: 1,944
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
Reply With Quote
  #5  
Old 27-11-2009
Member
 
Join Date: May 2008
Posts: 2,373
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 );
}
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #6  
Old 27-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,507
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("");
          }
        }
      }
}
Reply With Quote
  #7  
Old 11-09-2011
Member
 
Join Date: Sep 2011
Posts: 1
Re: How to write program to generate a pascal triangle in java?

hey also show output...
Reply With Quote
  #8  
Old 11-09-2011
Member
 
Join Date: Dec 2007
Posts: 2,218
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to write program to generate a pascal triangle in java?"
Thread Thread Starter Forum Replies Last Post
How to write java program to print pyramid of stars? Nadiaa Software Development 6 19-08-2011 05:03 PM
Need help to write this program in java? frkadeel Software Development 1 01-12-2010 02:58 PM
How to write string data to file using java program? Linoo Software Development 5 21-01-2010 08:26 PM
How to write java program to find factorial of number? Balamohan Software Development 5 28-11-2009 09:14 PM
how to write program on palindrome in java? Linoo Software Development 3 26-11-2009 04:19 PM


All times are GMT +5.5. The time now is 04:51 AM.