Here I have shared simple program for pascal triangle in C and C++.
Basically Pascal’s triangle is a triangular array of binomial coefficients. An example for how pascal triangle is generated is illustrated in below image. If you have any doubts then you can ask it in comment section.
Each number is the sum of the two directly above it.
Program for Pascal Triangle in C
#include<stdio.h> //function to calculate factorial long fact(int x) { int i; long f=1; for(i=1;i<=x;++i) { f=f*i; } return f; } int main() { int i,j,k,n; printf("How many lines? "); scanf("%d",&n); for(i=0;i<n;++i) { //loop to print spaces at starting of each row for(j=1;j<=(n-i-1);++j) { printf(" "); } //loop to calculate each value in a row and print it for(k=0;k<=i;++k) { printf("%ld ",fact(i)/(fact(i-k)*fact(k))); } printf("\n"); //print new line after each row } return 0; }
Program for Pascal Triangle in C++
#include<iostream> using namespace std; //function to calculate factorial long fact(int x) { int i; long f=1; for(i=1;i<=x;++i) { f=f*i; } return f; } int main() { int i,j,k,n; cout<<"How many lines? "; cin>>n; for(i=0;i<n;++i) { //loop to print spaces at starting of each row for(j=1;j<=(n-i-1);++j) { cout<<" "; } //loop to calculate each value in a row and print it for(k=0;k<=i;++k) { cout<<fact(i)/(fact(i-k)*fact(k))<<" "; } cout<<"\n"; //print new line after each row } return 0; }
Output
good
good… can you post the code for pascal triangle in c#.net?
hey when i typed nos. of lines = 20
some negative nos. came whyy??
Check the second for loop
palindrome nos. are those nos. whose reverse is same ….crazy programmer is my program right … i mean can i get full marks for it in exam as it works.. |
|
v
in dev c++ reply me
see it
0)=================>
#include
int main()
{ using namespace std;
int a,b,c,j,f;
cout<<" enter a no. at hundred no."<<"\n"
<<"enter a no. at tense place "<<"\n"
<<"enter a nos. at unit place "<>a>>b>>c;
j=a*100+b*10+c;
cout<<" the entered nos. is "<< j<<"\n";
f=c*100+b*10+a;
cout<<" the reverse nos. is "<<f;
if(j==f)
{
cout<<"\n"<<" as we can see its palindrome number";
}
else
{
cout<<"\n"<<" as we can see it is not palindrome";
}
return 0;
}
instead of using alphabets as variables name use words it will make program easy to read