Pascal's Triangle ( how to to make spaces b/w nums).

Aug 17, 2012 at 5:10am

Hi , I am trying to make pascal's triangle (http://en.wikipedia.org/wiki/Pascal%27s_triangle)

The output if n value is 4 is :



1
1 1
1 2 1
1 3 3 1

But my output is like
1
11
121
1331

My program is below :

#include <iostream>

using namespace std;

long factorial(int n)
{

int result = 1;
for(int c = 1;c<=n;c++)
{
result = result * c;
}
return result;
}

int main()
{

int n;
cout<<"Enter the n Value upto what";
cin>>n;

// To print the rows :

for(int i=0;i<n;i++)
{

// Spaces generation:
for(int c=0;c <= (n-i-2);c++)
{

cout<<" ";
}
// For printing the numbers :
for(int c=0;c<=i;c++)
{
cout<<factorial(i)/(factorial(c) * factorial(i-c));

}
cout<<endl;

}


}



Please say me how to print the spaces in between the numbers.



Aug 17, 2012 at 5:31am
Here i solved your problem..
1
2
3
4
5
6
7
8
9
10
// To print the rows :
    for(int i=0;i<n;i++)
    {
        // For printing the numbers :
        for(int c=0;c<=i;c++)
        {
            cout<<factorial(i)/(factorial(c) * factorial(i-c))<<" ";
        }
        cout<<endl;
    }
Aug 17, 2012 at 5:56am
Thanks man. Very simple but Important.
Last edited on Aug 17, 2012 at 5:56am
Aug 17, 2012 at 6:41am
This is code is not correct because its not producing the same output as Pascal's Triangle.
Please post the correct code soon.
Aug 17, 2012 at 6:59am
how to use the while loop,for loop,do while loop....plz explain it...........
Topic archived. No new replies allowed.