Hello everyone,
im new here and dont know yet most of the policies.. but i have been reading lots of your C++ codes, mostly, codes made by Narue.... i wonder if you someone could help me make a code for pascal's triangle wherein the ouput would look like a triangle and not like this:
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
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1


hope someone could help.. thanks a lot!!!

i see....

can someone just teach me how to align the output to center?

here is my codes for pascal's triangle... but i do not know how to center align the output...

#include <iostream.h>
int main()
{
  int n;
  cin >> n;
  for (int y = 0; y < n; y++)
  {
    int c = 1;
    for (int x = 0; x <= y; x++)
    {
      cout << c << " ";
      c = c * (y - x) / (x + 1);
    }
    cout<<endl;
  }
  cout<<endl;
  return 0;
}

Think of it not as alligning to the center. Think of it as outputing a certain number of spaces and then the numbers of a line in the triangle.
For example
7 Spaces+1 8 28 56 70 56 28 8 1
4 Spaces + 1 9 36 84 126 126 84 36 9 1
0 Spaces + 1 10 45 120 210 252 210 120 45 10 1
will give you the output something like

1 8 28 56 70 56 28 8 1
    1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1

The best thing to do in my opinion will be to count the length of characters output in the last row, and then use the width method to format the above rows with respect to the last row.

The following code gives a crude Triangle like output. It gets uglier when the number of line increases.

#include <iostream>
int main()
{
  int n;
  std::cin >> n;
  for (int y = 0; y < n; y++)
  {
    int c = 1;
    std::cout.width(n - y );// Added only this.
    for (int x = 0; x <= y; x++)
    {
      std::cout << c << " ";
      c = c * (y - x) / (x + 1);
    }
    std::cout<<std::endl;
  }
  std::cout<<std::endl;
  return 0;
}

thanks a lot.. i'll try it.. by the way, what does std mean? i havent encountered that yet...

thanks a lot.. i'll try it.. by the way, what does std mean? i havent encountered that yet...

cin , cout , endl and the such are members of the std namespace. So in new compilers, you have to tell it to look inside the std namespace, otherwise you will get a compile error. Since it maybe tedious to write std:: all the time, you can use

using namespace std;

after before the main function and you can just type cout , cin , endl . Read this for more information. You can search for "C++ Namespaces std" in google too.

Also in new compilers #include <iostream.h> will give you an error. The correct way is #include <iostream> . Without the .h .

here is my codes for pascal's triangle... but i do not know how to center align the output...

#include <iostream.h>
int main()
{
  int n;
  cin >> n;
  for (int y = 0; y < n; y++)
  {
    int c = 1;
    for (int x = 0; x <= y; x++)
    {
      cout << c << " ";
      c = c * (y - x) / (x + 1);
    }
    cout<<endl;
  }
  cout<<endl;
  return 0;
}

Can you please explain a little how you derived the formula c = c * (y - x) / (x + 1); ....just to get the idea of what steps to do in order to reach this formula

>Can you please explain a little how you derived the formula c = c * (y - x) / (x + 1); ....just to get the idea of what steps to do in order to reach this formula

Dude, are we going the reinvent the whole history of maths?
No!

BTW, Do you think that reviving a three year old thread is such a good idea?
I don't think so.
(Just check out this and if you've any more questions, then you should start a new thread about it)

Comments
Bad narue, bad
Works for me
It's a legitimate question. Are you really trying to accomplish something by flaming the guy, or just being an ass?

>It's a legitimate question. Are you really trying to accomplish something by flaming the guy, or just being an ass?
I didn't want to blame anyone, Narue, but didn't you once write the following?

Search for your answer first!

If you're having a problem, chances are good that someone else has had the same problem. Please search the forum for existing answers before starting a new thread. Nothing is more irritating to a long time member than to answer the same question for the umpteenth time because someone didn't use the forum's search feature.

Don't resurrect old threads!

Inevitably, if you use the search feature you'll stumble upon an interesting thread that you think you can add to. Resist the temptation to post until you've looked at the post date for the most recent post in that thread. It's possible that the thread is years old and you have no business bumping it to the top of the list. A thread is considered old after two months.

Let dead threads lie, don't post in them!

Source: http://www.daniweb.com/forums/thread78223.html
:P

Comments
Zzzzing!!!!!

I am not good at using TEX, because I have never used it before.

However I would like you all to make an assumption Let Capital 'C' mean or C mean the Combinations. Or (n!)/(n-r)! * r!

Where '!' represents the factorial values.

So next we just take the normal

nCr = n!/n-r!*r!

Then now lets consider nC(r+1),

nC(r+1)= n!/(n-(r+1))!*(r+1)!

Now We can then derieve:
nCr+1=n!/(n-r-1)!*(r+1)!

So derieving more out of it. we get

=> n!*(n-r)/(n-r)*(r+1)!

=>n!*(n-r)/(n-r)!*(r)!* (r+1)

=>n!/(n-r)!*(r!) * n-r/(r+1)

Which is nothing but

=> nCr * n-r/(r+1)

So what we actuall are doing in the code is that considering.

int c=1 /*let c= nC0 ==1*/

But We are using the variables.

x==n in the formula and y==r in the formula.

By that we can just derieve the value.

Because the Pascal triangle is nothing but

The Combinations nC0 , nC1 , nC2 ................. nC(r-1), nCr

i need to create/have a program/code that display pascal triangle in a simple ouput display.....help me.
plsSs....

i need to create/have a program/code that display pascal triangle in a simple ouput display.....help me.
plsSs....

Read post no. 11.

Thread closed due to multiple useless resurrections in the last 3 years.

#include<stdio.h>
void main()
{int n,i,j,k,a=1,b=1,c=1,nc;
 printf("enter the number:");
 scanf("%d",&n);
 nc=n;
 for(i=0;i<n;i++)
 {for(k=0;k<nc;k++)
  printf("  ");
  printf("1");
  for(j=0;j<i;j++)
  {
    c=c*(i-j)/(j+1);
    printf("   %d",c);

  }
  printf("\n");
  nc--;
 }
}

//akhil ratheesh, output as pyramid

#include <stdio.h>
int main()
{
    int x,i,j,z[40][40];
    scanf("%d",&x);
    for(i=0;i<x;i++)
    {
        for(j=0;j<i;j++)
        {   
            if(i==1)
            {
                z[i][j]==1;

            }
            else if(j==x || j==1)
            {
                z[i][j]==1;


            }
            else if(j>1 || j<x)
            {
                z[i][j]=z[i-1][j-1]+z[i-1][j];

            }


        }

    }
    for(i=0;i<x;i++)
    {
        for(j=0;j<i;j++)
        {
            printf("%d",z[i][j]);

        }
        printf("\n");
    }




}

this mine,it false,can you help me,sorry for my bad english T T

Hmm actually wrote something for this a loooong time ago, I'm sure it's very helpful.

#include <iostream>
#include <vector>
#include <iomanip>
const int ROWS = 8; // Change "ROWS" to set the height of the pyramid.
int v (const std::vector<int>& d, const int n, const int k) { return (k > n || n < 0 || k < 0) ? 0 : d[(((n * n) + n) / 2) + (n - k)]; }
int o (int n) { int d = 0; while (n > 0) { n /= 10; d++; } return d; }
int main(void) {
    std::vector<int> d;
    d.push_back(1);
    for (int r = 1; r < ROWS; r++) for (int c = r; c >= 0; c--) d.push_back(v(d, r - 1, c - 1) + v(d, r - 1, c));
    int b = o(v(d, ROWS - 1, (ROWS - 1) / 2)) + 1;
    for (int r = 0; r < ROWS; r++, std::cout << std::endl) for (int c = r, j, u; j = v(d, r, c), u = (b - o(j)) / 2, c >= 0; c--)
                std::cout << std::setw((c == r) * (b * ((ROWS - 1) - r))) << ""<< std::setw(u) << "" << j << std::setw(b - (u + o(j))) << "" << std::setw(b) << "";
    return 0;
}

It should adjust the widths accordingly for all size pyramids and attempts to center the numbers within their section, if I remember correctly. Due to console window limitations you should probably echo it to a textfile:

Application >> file.txt

#include<stdio.h>
void func(int i,int n)
{
int a;
if(i==0||i==n-1)
{
a=1;
}
else
a=func(i-1,n-1)+func(i,n-1);
return a;
}
void main()
{
int n,p,i,c,d=0;
printf("Enter the number");
scanf("%d",&p);
printf("pascal's triangle of hight %d is",p);
for(n=1;n<=p;n++)
{
for(i=0;i<n;i++)
{
if(i=0)
c=p-n;
else
c=p-n+i+1;
for(b=0;b<c;b++)
{
printf("\t");
}
printf("%d",func(i,n));
d=d+func(i,n);
}
printf("\n");
}
b=p(p+1)/2;
printf("no. of members in this pascal's triangle is %d \n",b);
printf("sum of all the members is %d",d);
return 0;
}
// Pascal Trangle Using byy arrray
#include<iostream.h>
#include<iomanip.h>
#define max 16
void main()
{ int a[max][max];
int n;
cout <<"Display Pascal Tringle shao:\n";
cout<<"Enter the value (<=15):";
cin>>n;
if (n>=16)
{ cout <<"Thge value is so big\n";
return;
}
if (n<0)
{ cout <<" Inlvalid value!";
return;
}
for ( int i=0;i<=n;i++)
a[i][0]=a[i][i]=1;
for (i=2;i<=n;i++)
  for (int j=1;j<i;j++)
  a[i][j]=a[i-1][j-1]+a[i-1][j];
    for (i=1;i<=n;i++)
     { cout<<" \n" ;
      for ( int j=0;j<=i;j++)
     cout<<setw(5)<<a[i][j];
     }
     }
This article has been dead for over six months. Start a new discussion instead.