Pages

  • Home
  • About & Contact
  • Archive
  • C Programming Examples

12 February 2012

C program to print Character Pascal triangle using array

Here is the program to print the Character pascal triangle using array.

C program to print Character Pascal triangle using array

program to print the Character pascal triangle using array


#include<stdio.h>
#include<conio.h>
void main()
{
        int arr[30],temp[30],i,j,k,l,n;           //using 2 arrays
       clrscr();
      
       printf("Enter the number of lines to be printed: ");
       scanf("%d",&n);
       temp[0]=1;
       arr[0]=1;

       for(j=0;j<n;j++)
      printf(" ");

       printf(" A\n");

       for(i=1;i<n;i++)
       {
      for(j=0;j<n-i;j++)
printf(" ");

for(k=1;k<i;k++)
{
  arr[k]=temp[k-1]+temp[k];
}

arr[i]=1;

for(l=0;l<=i;l++)
{
 printf(" %c",((arr[l]))%26+64);   //mod with 26 as there are 26 characters
                                                                  //and added 64 as ascii value of A is 65    
 temp[l]=arr[l];
}

printf("\n");
}
       getch();
}

Expanation: Firstly I recommend you to see how does pascal triangle works.
In this program i have use the property of pascals triangle that sum of two numbers in above row i.e (sum of n-1 and n of above column is current n value).I have highlighted that line with red colour.here i have used temp array to store the values of previous line so that I can use it for getting the sum.

Similar programs:

c program to print pascal triangle [using arrays and for loop]




Share your views regarding the given post via the comments section given below.
If you liked the post, please '+1' it & share it on other social networks.
Thank You.
-Romil Shah

No comments:

Post a Comment

Back To Top