Lets try out the c code for "pascal triangle" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Lets try out the c code for "pascal triangle"

17th Mar 2017, 9:27 AM
Arpit Saini
Arpit Saini - avatar
3 Answers
+ 2
#include <iostream> using namespace std; int main() { long int row, i,tab,k; cout <<"Enter the number of rows"<<endl; cin>>row; long int arr[row][2*row+1]; for(i=0;i<row;i++) //this loop is to move to next row { for(tab=0;tab<row-i;tab++) // this is to give tabs { cout<<"\t" ; } for(k=0;k<=i;k++) { arr[i] [row-i+k]=1; //putting first element of row as 1 if(i>=2 && k>=1) { arr[i] [row-i+k]=arr[i-1][row-i+k]+arr[i-1][row-i+k+1]; // adding the elements of precious row if(k==i) { arr[i] [row-i+k]=1; // putting the last element of a row 1 } } cout<<arr[i][row-i+k]<<"\t\t"; } cout<<endl; } return 0; }
18th Mar 2017, 10:35 AM
Arpit Saini
Arpit Saini - avatar
+ 1
@Navaneeth thanks for posting the code. This is very interesting code I have come across on pascal triangle. how did you came up with the logic?
17th Mar 2017, 7:39 PM
Arpit Saini
Arpit Saini - avatar
+ 1
@ Navaneeth this is my code of pascal triangle using 2d array let me know if the code can be optimized.
18th Mar 2017, 10:35 AM
Arpit Saini
Arpit Saini - avatar