Waht is the sample code to print pascals traingle in c

Showing Answers 1 - 3 of 3 Answers

baseersd

  • Jun 19th, 2007
 

The code mentioned here is not exactly of pascal triangle but it gives the output in the following form.
1
11
121
1331
::
::
:::

#include<stdio.h>

int main()
{
  int n,y,x;
  printf("Enter the limit ::");
 scanf("%d",&n);
  for (y = 0; y < n; y++)
  {
    int c = 1;
    for (x = 0; x <= y; x++)
    {
      //cout << c << " ";
      printf("%d ",c);
      c = c * (y - x) / (x + 1);
    }
//    cout<<endl;
      printf("n");
  }
//  cout<<endl;
    printf("n");
    getch();
  return 0;
}


Another method which is not that much efficient

#include<stdio.h>
int count=0;
int limit;
void printarray(int *arr,int i)
{
   
     int arr1[20]={0};
     int j;
     arr1[0]=1;
     for(j=1;j<=i;j++)
                     arr1[j]=arr[j-1]+arr[j];
     arr1[j]=1;
     for(i=0;i<j;i++)
     {
     printf("%4d ",arr1[i]);
     arr[i]=arr1[i];
     }
}

int main()
{
    int arr[20]={0};
    printf("nEnter the limit");
    scanf("%d",&limit);
    while(count<limit)
    {
    printarray(arr,count);
    count++;
    printf("n");
    }
    getch();
    return 0;
}



 

  Was this answer useful?  Yes

Naveen Talasila

  • Aug 15th, 2007
 

#include
int main()
{
int i,j,k,count =0;
int *a =NULL;scanf(
"%d",&count);
a = (int*)(malloc((sizeof(int))*count));
for (i=0;i
{
for (j= 0;j<=i;j++)
{
a[j] = a[i]+1;
((j>0) && (j==i)) ? printf("1") : printf("%d",a[j]);
a[i]=i-1;
}
printf("n");
}
free(a);
}


  Was this answer useful?  Yes

venkat

  • May 11th, 2016
 

Code
  1. span style="font-style: italic;">*****                                                      

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions