Wednesday, February 20, 2019

Special alphabet pattern as per user request


Special Alphabet Pattern

This program is written for special request received from blogger "Sree". Thanks to Sree for referring this blog.

Following C program will print special alphabet pattern series.

For n=3, expected output is

A B C B A
  B C B
    C

For n=4, expected output is

A B C D C B A
  B C D C B
    C D C 
      D

#include<stdio.h>
int main() 
{
  int i, j, k, n;
  printf("Enter your number:");
  scanf("%d", &n);
  for (i=0; i<n; i++) 
  {
     //logic for intial spaces
     for(j=0; j<i; j++) 
          printf("  ");

     //printing first half in a row(eg for n=4; ABCD)
     for(k=65+i; k<65+n-1; k++)
           printf("%c ", k);
     //printing second half in a row(eg for n=4; CBA)

     for( ; k >=65+i; k--)
           printf("%c ", k);

     printf("\n");
  }
  return 0; 
}

Output of program

Enter your number:4

A B C D C B A
  B C D C B
    C D C
      D


Read more Recommended Patterns Programs here.


No comments:

Post a Comment