2.02.2012

String triangle macro

Q. Write a macro to display the string INDIA in the following ashion:
I
IN
IND
INDI
INDIA
INDIA
INDI
IND
IN
I


Ans.


/* c program to display the string as given in the question using macro*/
#include<stdio.h>
#include<conio.h>
#define MACRO for(x=0; x<5; x++)          \
              {                           \
               y=x+1;                     \
               printf("%-5.*s\n",y,str);  \
              }                           \
              for(x=4; x>=0; x--)         \
              {                           \
               y=x+1;                     \
               printf("%-5.*%s\n",y,str); \
              }
int main()
{
 int x,y;
 static char str[]="INDIA";
 printf("\n");
 MACRO;
 getch();
 return 0;
}


OUTPUT:-



I
IN
IND
INDI
INDIA
INDIA
INDI
IND
IN
I


You might also like:

  1. Write above program using function
  2. Write above program in simple way 

1 comment: