1.03.2013

Number Character Triangle

Q. Write a C program to print the following number character triangle:

1      a
21     ba
321    cba
4321   dcba
54321  edcba

Ans.

/*c program for number character pyramid*/
#include<stdio.h>
int main()
{
 int num,n,r,c,sp;
 char p,rch='a';
 printf("Enter no of rows: ");
 scanf("%d", &num);
 n=num;
 for(r=1; num>=r; r++,n--,rch++)
 {
  for(c=r; c>=1; c--)
    printf("%d",c);
  for(sp=0; sp<=n ;sp++)
    printf(" ");
  for(p=rch; p>='a'; p--)
    printf("%c",p);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of number-character pyramid C program
Figure: Screen shot for number-character pyramid C program

1 comment: