Sunday, October 1, 2023

8.Easy inverted triangle pyramid

                     

 have added numbers to make you understand how we will create this pattern using for loop
Also refer 6.Inverted triangle pattern with left spacing because it is derived from it
  • As we know we first see how many lines of code needs to be printed, in this case it is 9 
  • Since we will take user input it can be simply n which will be taken from user
  • Along with the stars space is also being printed
  • Inside outer i for loop there will be two loops one for star and another for space
  • For space we will use variable s
  • For i=1 ,the number of space is 0,for i=4 it is 3
  • Here we can see space is being printed i-1 times
  • So s will iterate from 1 to i-1
  • For the pattern star i=1 the  number of stars printed are 9 i.e n
  • For i=3,number of stars printed are 3 to n i.e  7
  • So j will iterate from i to n
  • One thing to observe is that there is a space after each star so remember to print that
  • j loop will print "* ". 
Code
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,n,s;
clrscr();
printf("Enter number of lines ");
scanf("%d",&n);
for(i=1;i<=n;i++)
    {
          for(s=1;s<=i-1;s++)
               {
                printf(" ");
                 }
          for(j=i;j<=n;j++)
               {
                 printf("* ");   
                   }
           printf("\n");
     }

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

No comments:

Post a Comment