Pascal Triangle
PROBLEM :
Given an integer K,return the kth row of pascal triangle.
Pascal's triangle is a triangular array of the binomial coefficients formed by summing up the elements of previous row.
Example of pascal triangle:
1
1 1
1 2 1
1 3 3 1
for K=3, return 3rd row i.e 1 2 1
Input:
The first line contains an integer T,depicting total number of test cases.
Then following T lines contains an integer N depicting the row of triangle to be printed.
Output:
Print the Nth row of triangle in a separate line.
Constraints:
1 = T = 50
1 = N = 25
Example:
Input
1
4
Output
1 3 3 1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void Pascal_Triangle(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
Pascal_Triangle(no) ;
cout<<endl ;
}
return 0;
}
void Pascal_Triangle(int no)
{
int i,j ;
int mat[no][no] ;
for(i=0;i<no;i++)
for(j=0;j<=i;j++)
{
if(j==0||j==i)
mat[i][j]=1 ;
else
mat[i][j]=mat[i-1][j-1]+mat[i-1][j] ;
}
for(i=0;i<no;i++)
cout<<mat[no-1][i]<<" " ;
}
---------------------------------------------------------------------------------
Given an integer K,return the kth row of pascal triangle.
Pascal's triangle is a triangular array of the binomial coefficients formed by summing up the elements of previous row.
Example of pascal triangle:
1
1 1
1 2 1
1 3 3 1
for K=3, return 3rd row i.e 1 2 1
Input:
The first line contains an integer T,depicting total number of test cases.
Then following T lines contains an integer N depicting the row of triangle to be printed.
Output:
Print the Nth row of triangle in a separate line.
Constraints:
1 = T = 50
1 = N = 25
Example:
Input
1
4
Output
1 3 3 1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void Pascal_Triangle(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
Pascal_Triangle(no) ;
cout<<endl ;
}
return 0;
}
void Pascal_Triangle(int no)
{
int i,j ;
int mat[no][no] ;
for(i=0;i<no;i++)
for(j=0;j<=i;j++)
{
if(j==0||j==i)
mat[i][j]=1 ;
else
mat[i][j]=mat[i-1][j-1]+mat[i-1][j] ;
}
for(i=0;i<no;i++)
cout<<mat[no-1][i]<<" " ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment