The First Program :
PASCAL TRIANGLE
The Pascal Triangle is one of the important and difficult programs to understand for a C beginner and if you are an Indian student pursuing Engineering you would feel the real struggle as it is made complicated using UNNECESSARY formula and multiple variables.
A Pascal Triangle looks like this:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
A Pascal Triangle gives the coefficients of the terms formed when two different variables are added or subtracted over a power.
Example:
(x+y)^0 = 1 ------ ( default case )
(x+y)^1 = 1.x + 1.y
(x+y)^2 = 1.x^2 + 2. xy + 1.y^2
(x+y)^3 = 1.x^3 + 3.x^2y + 3.xy^2 + 1.y^3
(x+y)^4 = 1.x^4 + 4.x^3y + 6.x^2.y^2 + 4.xy^3 + 1.y^4
Basically , the coefficients of 1st and last terms for any power will be 1 .
To get the remaining terms' coefficients :
We add the y and y+1 terms' coefficient values of 'x' to get the y+1 value of 'x+1' i.e
One thing that is important to remember is that power 'x' has x+1 terms and power 'x+1' has 'x+2' and that is how the 1s present at the start and end will be handy to compute the coefficients of the next powers.
PROGRAM :
#include<stdio.h>
void main()
{
int i,j,k,l,n;
printf("Enter the power whose coefficients are required:");
scanf("%d",&n);
int a[n],b[n],temp[n];
a[0]=temp[0]=1; // for the first 1 value in all powers
a[1]=1; // only for the first case
printf("1\n"); // for the default case
for(j=2;j<n+1; )
{
for(i=0;i<j;i++)
{
printf("%d\t",a[i]); //printing of coefficient values
}
printf("\n");
j=j+1;
for(k=0;k<j;k++)
{
if(k==j-1)
{
temp[k]=1; // for the last 1 value in all powers
}
else
temp[k+1]=a[k+1]+a[k]; //result of addition of coefficients into temp array
} //so that no loss of data happens
for(l=0;l<j;l++)
{
a[l]=temp[i]; //updated values into array.
}
}
}
Input:
3
Output:
1
1 1
1 2 1
1 3 3 1
Input:
1
Output:
1
1 1
* The above written code is an original one.
* Corrections and suggestions are appreciated.
PASCAL TRIANGLE
The Pascal Triangle is one of the important and difficult programs to understand for a C beginner and if you are an Indian student pursuing Engineering you would feel the real struggle as it is made complicated using UNNECESSARY formula and multiple variables.
A Pascal Triangle looks like this:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
A Pascal Triangle gives the coefficients of the terms formed when two different variables are added or subtracted over a power.
Example:
(x+y)^0 = 1 ------ ( default case )
(x+y)^1 = 1.x + 1.y
(x+y)^2 = 1.x^2 + 2. xy + 1.y^2
(x+y)^3 = 1.x^3 + 3.x^2y + 3.xy^2 + 1.y^3
(x+y)^4 = 1.x^4 + 4.x^3y + 6.x^2.y^2 + 4.xy^3 + 1.y^4
Basically , the coefficients of 1st and last terms for any power will be 1 .
To get the remaining terms' coefficients :
We add the y and y+1 terms' coefficient values of 'x' to get the y+1 value of 'x+1' i.e
- 2nd and 3rd coefficient values of power 4 are 4 and 6 respectively.
- Now 3rd term's coefficient of power 5 is 10 which is 4+6.
One thing that is important to remember is that power 'x' has x+1 terms and power 'x+1' has 'x+2' and that is how the 1s present at the start and end will be handy to compute the coefficients of the next powers.
PROGRAM :
#include<stdio.h>
void main()
{
int i,j,k,l,n;
printf("Enter the power whose coefficients are required:");
scanf("%d",&n);
int a[n],b[n],temp[n];
a[0]=temp[0]=1; // for the first 1 value in all powers
a[1]=1; // only for the first case
printf("1\n"); // for the default case
for(j=2;j<n+1; )
{
for(i=0;i<j;i++)
{
printf("%d\t",a[i]); //printing of coefficient values
}
printf("\n");
j=j+1;
for(k=0;k<j;k++)
{
if(k==j-1)
{
temp[k]=1; // for the last 1 value in all powers
}
else
temp[k+1]=a[k+1]+a[k]; //result of addition of coefficients into temp array
} //so that no loss of data happens
for(l=0;l<j;l++)
{
a[l]=temp[i]; //updated values into array.
}
}
}
Input:
3
Output:
1
1 1
1 2 1
1 3 3 1
Input:
1
Output:
1
1 1
* The above written code is an original one.
* Corrections and suggestions are appreciated.