0

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.

6
Contributors
5
Replies
6
Views
12 Years
Discussion Span
Last Post by sunyifei23
0

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
}

Edited by Nick Evan: Fixed formatting

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.

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

Edited by manali04: NA

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.