Character Pyramid

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

A
BA
ABA
BABA
ABABA

Ans.

/*c program for character pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c;
 char ch='A',st='B';
 printf("Enter number of rows: ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=r; c>=1; c--)
  {
   if(c%2==0)
     printf("%c", st);
   else
     printf("%c", ch);
  }
  printf("\n");
 }
 return 0;
}

The output of above program would be:

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


Comments