Monday, 2 February 2015

Pattern printing in C with examples

Pattern printing in C with examples:

DCBA

 CBA

  BA

   A


The below code helps to print the above format

#include "stdafx.h"

#include <stdio.h>

#include <stdlib.h>

int main ()

{

  int iNum, iIndex, Ind1, iSpace = 0;

  char ch, chTemp;

  printf ("Enter the number of lines do you want to print");

  scanf("%d", &iNum);

  ch = 'A'+iNum-1;

  chTemp = ch;

  for ( Ind1 = iNum ; Ind1 >= 1 ; Ind1-- )

  {

     for ( iIndex = 1 ; iIndex <= iSpace; iIndex++)

     printf(" ");

     iSpace++;

     for ( iIndex = 1 ; iIndex <= Ind1 ; iIndex++ )

     {

       printf("%c",chTemp);

       chTemp--;

     }

     printf("\n");

     ch--;

     chTemp = ch;

  }

  getchar ();

  return 0;

}

The output of the above program is
Enter the number of lines do you want to print4

DCBA

 CBA

  BA

   A


No comments: