954,219 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

pascals triangle

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.

let us c
Newbie Poster
17 posts since Aug 2004
Reputation Points: 10
Solved Threads: 1
 

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

ganesh
Newbie Poster
2 posts since Aug 2004
Reputation Points: 12
Solved Threads: 0
 

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.

iamboredguy
Newbie Poster
23 posts since Aug 2004
Reputation Points: 12
Solved Threads: 0
 

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

manali04
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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.

mike_2000_17
Posting Virtuoso
Moderator
2,126 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 456
 

I believe you can use the coefficient formula.

sunyifei23
Newbie Poster
11 posts since Jul 2010
Reputation Points: 10
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You