Sunday, October 1, 2023

14.Hollow Number 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 numbers 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
  • A difference we see in this pattern is that stars are not being printed
  • If we observe the numbers carefully we can understand those are just j values
  • So true part will print j values
  • 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("%d",j);
                                       }
                                else
                                       {
                                         printf(" ");
                                         }   
                   }
           printf("\n");
     }

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

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

12.Hollow rectangle

 

Steps

  • First we will count the number of lines i.e 9
  • So i will iterate from 1 to n // n is the value given by user for number of lines
  • Now for inner loop j we need to see how many times the pattern is printed i.e 15
  • Let it be m
  • So j will iterate from 1 to m 
  • Now we need to see where stars are printed and where space is printed
  • First we can see for the first line (horizontal) and last line (horizontal)
  • These two conditions will be i==1 for first line and i==n for last line
  • Next we see that each time first star is printed and last star is printed (vertical)
  • These two conditions will be j==1 for first vertical line and j==m for last line
  • These four conditions can be used together using logical or 
  • It will be used in a if  condition inside j loop
  • True part will print * 
  • False part will print space
  • Remember to add "\n"

Code

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

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

 

11.Hollow square



Steps

  • First we will count the number of lines i.e 9
  • So i will iterate from 1 to n  // n is the value given by user for number of lines
  • Now for inner loop j we need to see how many times the pattern is printed i.e 9
  • It is same as n
  • So j will iterate from 1 to n as well
  • Now we need to see where stars are printed and where space is printed
  • First we can see for the first line (horizontal) and last line (horizontal)
  • These two conditions will be i==1 for first line and i==n for last line
  • Next we see that each time first star is printed and last star is printed (vertical)
  • These two conditions will be j==1 for first vertical line and j==n for last line
  • These four conditions can be used together using logical or 
  • It will be used in a if  condition inside j loop
  • True part will print * 
  • False part will print space
  • Remember to add "\n"

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<=n;j++)
               {
                    if(i==1 || i ==n || j==1 || j==n)
                          {
                               printf("*");
                            }
                    else 
                           {
                            printf(" ");    
                             }   
                   }
           printf("\n");
     }

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

 

10.Inverted Pyramid

 

  • As always we  count the number of times the pattern is printed so it is 9
  • In this case we will iterate from n-1 to 0 i.e n times
  • It is done because it will be easy to use the logic used in 9.Pyramid
  • For space,first for n-1 there is no space 
  • For n-2 i.e 7 there is one space and so on
  • So for s we can iterate from i to n-2 
  • So for i=8 n-2 is 7 so condition(8≤7) is false and no space is printed
  • For i=7 n-2 is 7 so condition (7≤7) is true so one space is printed
  • It will be like this for(s=i;s<=n-2;s++) //You can also use s<n-1 that is same
  • Now for the main pattern for i=8, stars printed are 17
  • For i=1, stars printed are 3
  • For i=0 it is only 1
  • We can find a pattern that the number of times they are printed is (2*i)+1 times
  •  So j will iterate from 1 to (2*i)+1
Code

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

Note:scanf is used for taking user input

9.Pyramid

 


  • First we count the number of times the pattern is printed i.e 9
  • In this we have an exception that we won't iterate from 1to n but from 0 to n-1
  • The outer for loop will iterate from 0 to n-1
  • Before we used to iterate from 1 to n-i but now initial i value is 0
  • So we will iterate from 1 to n-i-1
  • Now for the main pattern for i=0 , stars printed is 1
  • For i=1 stars printed are 3
  • For i=2 stars printed are 5
  • We can find a pattern that is (2*i)+1
  • You can check by substituting the i value
  • So j will iterate from 1 to (2*i)+1
  • Remember to print "\n" to make it move to next line
Code

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

Note:scanf is used for taking user input

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

7.Easy triangle pyramid

 



I have added numbers to make you understand how we will create this pattern using for loop
Also refer 4.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 8,for i=4 it is 5
  • Here we can see space is being printed n-i times
  • So s will iterate from from 1 to n-i
  • The stars that are printed are same as i
  • If we observe carefully we can see that there is a space after every star that is printed
  • So the j loop will iterate from 1 to i but now it will print
  • "* " i.e star along with space each time
  • Remember to print "\n" to make it move to next line

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<=n-i;s++)
                {
                   printf(" ");
                  }
          for(j=1;j<=i;j++)
               {
                 printf("* ");   
                   }
           printf("\n");
     }

getch();
}
Note: scanf is used for taking user input,s is for space , j for pattern