+ 1
Lets try out the c code for "pascal triangle"
3 Antworten
+ 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;
}
+ 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?
+ 1
@ Navaneeth this is my code of pascal triangle using 2d array let me know if the code can be optimized.