Pages

Saturday, 4 February 2017

Hollow Triangle Program C Language

Write a program which display a hollow triangle shape using asterisk character with while and for separately?

Hollow Triangle Program C Language Code

/*   ################################
     Girfa Student Help
     Hollow Triangle
     for more visit : http://girfahelp.blogspot.in/p/c-language.html
     ################################

*/
#include<stdio.h>
void main()
{
     int i,j,sp=10,k=1,m=1,size;
     clrscr();
     printf("Enter size of hollow triangle>> ");
     scanf("%d",&size);
     i=1;
     sp=size;
     while(i<size)
     {
           j=1;
           while(j<=sp)
           {
                printf(" ");
                j++;
           }
           if(i==1)
                printf("*");
           else
           {
               j=1;
               printf("*");
               while(j<=m)
               {
                printf(" ");
                j++;
               }
               printf("*");

               m+=2;
           }
           printf("\n");
           i++;
           sp--;
           k+=2;
     }
     /* Triangle base print */
     i=1;
     while(i<=sp)
     {
           printf(" ");
           i++;
     }
     j=1;
     while(j<=k)
     {
           printf("*");
           j++;
     }
     /* For loop Implementation*/
     printf("\nFor Loop Implementation\n");
     for(i=1,sp=size,k=1,m=0;i<size;i++,k+=2,sp--,m+=2)
     {
           for(j=1;j<=sp;j++)
                printf(" ");
           if(i==1)
                printf("*");
           else
           {
                printf("*");
                for(j=1;j<m;j++)
                     printf(" ");
                printf("*");
           }
           printf("\n");
     }
     /* Print base */
     for(i=1;i<=sp;i++)
           printf(" ");
     for(i=1;i<=k;i++)
           printf("*");


     getch();
}


No comments:

Post a Comment