Sunday, October 1, 2023

13.Hollow Triangle

 

Steps

  • Let's count the number of lines i.e 9 so we take it as n
  • So i will iterate from 1 to n
  • The pattern printed never exceeds i
  • For i=1 it is 1, i=9 pattern is there till 9
  • So j loop will iterate from 1 to i
  • Now coming to the pattern
  • We will see where stars are being printed 
  • First vertical line i.e for j=1 each time
  • Next horizontal line i.e i=n (i=9 in our example)
  • Finally there is a diagonal values 1==1,2==2 so it is i=j
  • We will use these conditions in a if condition using logical or
  • True part will print *
  • False part will print space

Code
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,n;
clrscr();
printf("Enter number of lines ");
scanf("%d",&n);
for(i=1;i<=n;i++)
    {
          for(j=1;j<=i;j++)
               {
                              if(i==n || j==1 || i==j)
                                     {
                                       printf("*");
                                       }
                                else
                                       {
                                         printf(" ");
                                         }   
                   }
           printf("\n");
     }

getch();
}
Note: scanf is used for taking user input

No comments:

Post a Comment