1. Member Join Date
Oct 2010
Posts
10
Rep Power
0

## a question about pascal triangle

can you help me about the Pascal triangle with depth (number of rows) 7 as follows:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

how can i do that with using loops in java??  Reply With Quote

2. ##  Originally Posted by rambo126 can you help me about the Pascal triangle with depth (number of rows) 7 as follows:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

how can i do that with using loops in java??
You can generate the next row given the current row; let the number of numbers in the current row be n and let 'r' be an array representing the current row. From right to left do this:

Java Code:
```r[n]= 1;
for (int i= n; --i > 0; r[i]+= r[i-1]);```
You have to figure out that for-loop because it's a complete give away.

kind regards,

Jos  Reply With Quote

3. Member Join Date
Oct 2010
Posts
10
Rep Power
0

## thanks jos but how can i do without using array?  Reply With Quote

4. ##  Originally Posted by rambo126 thanks jos but how can i do without using array?
Why don't you want to use an array? A single array of the size equal to the height of the triangle is enough and the computations are extremely cheap. The alternative if that use calculate C(n, k) for each element in row n, element k but that would be much more expensive and involves quite a bit of cleverness to circumvent the obvious limit of 13 (13! is the max number you can store in an int).

kind regards,

Jos  Reply With Quote

5. Member Join Date
Oct 2010
Posts
10
Rep Power
0

## i want to do it by using loops because i do not have enough information about arrays and also the question asks me solve the problem by using loops.. You said: The alternative if that use calculate C(n, k) for each element in row n, element k. Could you clarify this?  Reply With Quote

6. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
17

## See for example the Wikipedia article on Combination.  Reply With Quote

7. Member Join Date
Oct 2010
Posts
10
Rep Power
0

## Java Code:
```import java.util.Scanner;

public class Lab05c{

public static void main(String args[]){
int m, n, f, mf, nf, nmf, k, result, i;

int maxRow=6;

for( n =0;n<=maxRow;n++){
k=n;
result=1;
for(i=2;i<=k;i++)

result=result*i

nf=result;

k=m=0;
result=1;
for(i=2;i<=k;i++)

result=result*i;

mf=result;

k=n-m;
result=1;
for(i=2;i<=k;i++)
result=result*i;

nmf=result;

f= nf/(mf*nmf);

for( m=0;m<=n;m++)

System.out.print(f);

System.out.println();
}
}

}```

i did like this and the output of this:
1
11
111
1111
11111
111111

what do i need to have right result? can you help me

Last edited by Fubarable; 10-30-2010 at 07:56 PM. Reason: Moderator Edit: code tags added  Reply With Quote

8. ## Sorry, but it's kind of hard to follow your code. Myself, if this were my project, I'd subdivide things by creating a small method that calculated the factorial of a number, n! = n * (n -1) * (n - 2)...., say
Java Code:
```   public static int factorial(int n) {
if (n == 0) {
return 1;
}
//... more code goes here. :-)
}```

And then again, using pbrockway's reference, create a very short method that calculates the combination:
Java Code:
```   public static int combination(int n, int k) {
// only one line of code here that uses the factorial method above a few times
}```

And then call the combination method inside of a nested for loop, and you're done.

Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

Java Code:
```[cod&#101;]  Reply With Quote