|
| ||||||||||
| Tags: java, java language, java programming, pascal triangle, while loop |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| How to write program to generate a pascal triangle in java?
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
| ||||
| ||||
| 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
| ||||
| ||||
| 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
| ||||
| ||||
| 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
| ||||
| ||||
| 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 |
|
#6
| ||||
| ||||
| 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
| |||
| |||
| Re: How to write program to generate a pascal triangle in java?
hey also show output... |
|
#8
| ||||
| ||||
| Re: How to write program to generate a pascal triangle in java? 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.
__________________ Education, Career and Job Discussions |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |