Page 1 of 1

## How to create a PascalTriangle Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=103291&amp;s=007febdc574ea9142082bde6723508d2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rishabhsharma

• D.I.C Regular

Reputation: 10
• Posts: 342
• Joined: 26-March 09

Posted 04 May 2009 - 09:27 AM

I assume you are familiar with Pascal Triangle, ‘cos you get assignments – w.a.p. to create Pascal Triangle. You feel like tearing pages when you are not through your program. Don’t worry to win over your tension, I am writing this tutorial.

Look closely at your pascal triangle. It looks like this….

…………1
………1…1
……1..2..1
…1..3..3…1
1.4..6….4..1

And so on till n(th) index..where n is the number of rows.
Each line of the triangle starts with 1 and ends with 1(amazing).
And each number after 1 is the sum of the two numbers direct above it. 1 + 2 = 3 , 1 + 3 = 4 , 3 + 3 =6 ,etc.

So now you know what really a pascal triangle is. So here we begin :

I assume this triangle to be in the grid of [n][n+(n-1)] where n is the number of rows.

Suppose, value of n is 3(say). Then we should have grid of [3][3+(3-1)] i.e. [3][5] [rows][columns]
So we declare the grid with :

Int[][] grid = new int [n][n+(n-1)];

And after that some variables… I will tell the use of these variables after some time, till then read it….

int help = n-1;
int as = 1;

Since, first number of any row starts with 1 and ends with 1, then, why not we shall firstly initialize every first and last number of any row with 1? This task will be accomplished with these lines…

```for ( int i = 0; i < n; i ++ )
{
int insertOne = n - ( i + 1 );
grid [ i ] [ insertOne ] = 1; /*Inserting every starting
number of the grid with 1*/
int inOne = n + i;
grid [ i ] [ inOne - 1 ] = 1; /*Inserting every ending
number of the grid with 1*/
} //rest will be zero

```

Rest will be zero, zero will be removed(don’t worry) at the time of printing it.

Next the crucial part of the program….
Since two lines of the program are already stored in the grid.
These two lines…
……1 //row 0
….1..1 //row 1

So we start the loop with 2 and continues till the ‘n’.

```
for ( int i = 2; i < n; i ++ ){
for ( int j = 1; j < i; j ++ ){
grid [ i ] [ help ] = grid [ i - 1 ] [ help - 1 ] +
grid [ i - 1 ] [ help + 1 ]; /*adding above
two grids*/

if ( i > 2 && j < ( i - 1)){
help += 2; /*increasing help by 2  if condition above is fulfilled*/
}
}
help -= as; //decreasing help by current value of as
as += 2; // increasing value of as by 2
}

```

Above snippet store numbers in the grid which is the sum of two numbers directly above it. In our example, where n is 3, and 2 row are already printed we only have to consider the grid [2][2].
(0,0)(0,1)(0,2)(0,3)(0,4)
(1,0)(1,1)(1,2)(1,3)(1,4)
(2,0)(2,1)(2,2)(2,3)(2,4)

So (2,2) is the sum of (1,1) and (1,3)
So we need the loop to run only once…

Use of help and as :
I have used help (x,help) and initialized it directly with n-1, as you can see firstly we need to calculate the index at (x(value of i ), help(3-1=2) ) …. And next time it is increased by two in the series if the loop runs for than one time….
Then outside the sub loop the value of help is decreased by the value of as and as is increased by 2, to get the suitable conditions for the next grid addresses.

How to print the grid :

So you know that the numbers has been stored in the grid of [3][5] and to print them is as easy to stand on one leg for a second.

Look at the snippet given below :

```
for(int i = 0; i < n; i ++ ) {//printing all values of the grid
for ( int j = 0; j < (n + ( n - 1 )); j ++ ){
if(grid[i][j] == 0 ){
System.out.print("	"); //printing space in case of zeros
}
else {
if(grid[i][j] < 10 )
System.out.print("   "+grid[i][j]);
if(grid[i][j]>9&&grid[i][j]<99)
System.out.print("  "+grid[i][j]);
if(grid[i][j]>99&&grid[i][j]<999)
System.out.print(" "+grid[i][j]);
if(grid[i][j]>999)
System.out.print(grid[i][j]);
}
}
System.out.println(); /*Inserting new line after
one run of a loop*/
}

```

First line of the main is for the rows of the grid :
As in our example it would look like
```
for(int i=0;i<3;i++)

```

And second loop is for the columns of the grid :
As in our example it would look like
```
for (int j = 0; j < 5; j ++ )

```

Eliminating every zeros from the grid :

To eliminate every zeros from the grid add these under second loop :
This snippet will check if the particular grid contains zero than it replaces it with spaces .
```
if(grid[i][j]==0)
{
System.out.print(“	“);
}

```

else print all the numbers in the grid ….

```
else {
if(grid[i][j] < 10 )
System.out.print("   "+grid[i][j]);
if(grid[i][j]>9&&grid[i][j]<99)
System.out.print("  "+grid[i][j]);
if(grid[i][j]>99&&grid[i][j]<999)
System.out.print(" "+grid[i][j]);
if(grid[i][j]>999)
System.out.print(grid[i][j]);	 }

```

And System.out.println(); to add new line after each run of the loop, outside the sub loop.

And your final product should look like this……

```
import javax.swing.*;

public class PascalTriangle2
{
public static void main(int n) //to get number of lines from user
{
if ( n > 20 ) //Restricted User To Give Input More Than 20
{
JOptionPane.showMessageDialog(null,"Input must be less than 20 " ,
Error!", JOptionPane.ERROR_MESSAGE); //Show Error Dialog
System.exit(0);
}
int[ ] [ ] grid = new int [ n ] [ n + ( n - 1 ) ]; //Declaring grid
int help = n-1;
int as = 1;

for ( int i = 0; i < n; i ++ )
{
int insertOne = n - ( i + 1 );
grid [ i ] [ insertOne ] = 1; /*Inserting every starting
point of grid with 1*/
int inOne = n + i;
grid [ i ] [ inOne - 1 ] = 1; /*Inserting every ending
point of grid with 1*/
} //rest will be zero

for ( int i = 2; i < n; i ++ )
{
for ( int j = 1; j < i; j ++ )
{
grid [ i ] [ help ] = grid [ i - 1 ] [ help - 1 ] +
grid [ i - 1 ] [ help + 1 ]; //adding above two grids
if ( i > 2 && j < ( i - 1))
{
help += 2; //increasing help by 2 if condition above is fulfilled
}
}
help -= as; //decreasing help by current value of as
as += 2; // increasing value of as by 2
}

for(int i = 0; i < n; i ++ ) //printing all values of the grid
{
for ( int j = 0; j < (n + ( n - 1 )); j ++ )
{
if(grid[i][j] == 0 )
{
System.out.print("	"); //printing space in case of zeros
}
else
{
if(grid[i][j] < 10 )
System.out.print("   "+grid[i][j]);
if(grid[i][j]>9&&grid[i][j]<99)
System.out.print("  "+grid[i][j]);
if(grid[i][j]>99&&grid[i][j]<999)
System.out.print(" "+grid[i][j]);
if(grid[i][j]>999)
System.out.print(grid[i][j]);
}
}
System.out.println(); //Inserting new line after one run of a loop
}
}
}

```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }