Print alphabet triangle pattern - C Program

Write a C Program to accept a string from user and generate following pattern. (e.g. string is "abcd”)
       
alphabet triangle

Solution:

For printing above output, first divide it into two parts. i.e

part 1                            part 2

We can print above pattern in two ways. We can either use ASCII value or do it directly using character.

1. Using ASCII value

#include<stdio.h>
int main()
{
      int i,j;
      for(i=97;i<=100;i++)        // This loop prints part-1 using ASCII value
      {
            for(j=97;j<=i;j++)
            {
                  printf("%c",j);
            }
            printf("\n");
      }
      for(i=99;i>=97;i--)          // This loop prints part-2 using ASCII value
      {
            for(j=97;j<=i;j++)
            {
                  printf("%c",j);
            }
            printf("\n");
      }
      return 0;
}


2. Directly using character value

#include<stdio.h>
int main()
{
      char i,j;          // Here, variable i and j are defined as character type.
      for(i='a';i<='d';i++)     // This loop print part-1 using character value
      {
            for(j='a';j<=i;j++)
            {
                  printf("%c",j);
            }
            printf("\n");
      }
      for(i='c';i>='a';i--)        //This loop prints part-2 using character value
      {
            for(j='a';j<=i;j++)
            {
                  printf("%c",j);
            }
            printf("\n");
      }
      return 0;
}


Output:

alphabet triangle