hi
i want to display pascals triangle on computer screen.the pascals triangle
goes like this. and what is logic.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1 and so on.

Recommended Answers

All 5 Replies

Hi there are many logics behind this problem. This is the simple logic. Though it is inefficient, this will give you a better approach to this program

a[0] = 1
for(i=0;i<n;i++){
for(j=0;j<=i;j++){
Value_to_print = a[j-1]+a[j];
/* Print the values in the order you want */
}
a[j] = 0
a[j-1] = Value_to_print
}

Well, you can try this code, but I don't know if it will work beyond n=9.

#include <iostream.h>
#include <conio.h>

/* This program creates a half pyramid of nos. Created on 12.6.2k3. */

void main()

{
 int n, i, r;
 cout<<"Enter the number of numbers: ";
 cin>>n;
 if (n>9)
 cout<<"\n This won't work out. ";
 else
 {
  for (i=1; i<=n; i++)
  {
   for (r=1; r<=i; r++)
   {
    cout<<r;
   }
   cout<<endl;
  }
 }
 getch();
}

/* Output:
Enter the number of numbers: 7
1
12
123
1234
12345
123456
1234567

*/

btw, I didn't know it was called a pascal's triangle. I used to refer to it just as a no. pyramid or whatever.

program by iamboredguy is not a Pascal Triangle !!
Pascal Traingle is expected to give following sequence.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

First logic feature to see is that it is always symmetric about the central value in one sequence, so you will probably just need to store or compute one half of each row (from start to middle) and repeat in reverse for the remainder of the row (taking into account even and odd number of elements).

The second logic feature to see is more evident in the normal display of the pascal triangle (forgive the ASCII art):

1
           / \
           1  1
          / \/ \
          1  2  1
         / \/ \/ \
         1  3  3  1
        / \/ \/ \/ \
        1  4  6  4  1
            ....

From this it's clear that the operation that joins a \/ segment is the addition. So like in ganesh's pseudo-code, the basic idea is to add two adjacent elements of the previous row to get the corresponding element of the next row (except for the first element which is always 1 and the centre element of an odd-row which might be twice the element closest to centre of the even-row above it).

Show us some code of your own, and we can help you further.

I believe you can use the coefficient formula.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.