Friday, 2 December 2011

String Triangle

Q. Write a program to display the string INDIA in the following fashion:

I
IN
IND
INDI
INDIA
INDIA
INDI
IND
IN
I

Ans.

/*program to display the string as given fashion*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int x,y;
 static char str[]="INDIA";
 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);
 }
 getch();
 return 0;
}

Output:-

I
IN
IND
INDI
INDIA
INDIA
INDI
IND
IN
I

Learn Also:
  1. Write above program using macro
  2. Write above program using function

No comments:

Post a Comment