Powered By Blogger

Thursday 12 March 2020

Different types of Pattern and Tree structure in 'C' programming (Making A "Pine Tree")


Welcome viewers  to another new ‘C’ structure or pattern ........
In this blog, we will print a "
Pine tree" with the help of ‘for loop’,’ while loop and some ‘if-else’ function. So, let's start...
First, let's look at the pattern of the tree.
TREE STRUCTURE IN 'C' PROGRAMMING
TREE STRUCTURE IN 'C' PROGRAMMING

We can see the tree has three same types of ‘Head part’ i.e upper part. And then followed by the five stared ‘Stock part’ and at the base of it, there is also five “=” character.
So, we need to first with the ‘head part’. As this part continues for three times, so we can make a ‘while loop’ with a condition of looping three times and printing the same pattern.
For ‘Stock part’ we need to use a simple  ‘for  loop’ that will print a ‘*’ with newline character at the time of each star printing.
For ‘Base’ there is also a simple ‘for loop’ that will iterate 5 times to print the “=” characters.
Now, let's see how the program is going on :
PROGRAMMING CODE IN 'C' TO PRINT A TREE PATTERN
PROGRAMMING CODE IN 'C'

(i)”HEAD PART” or “UPPER PART”:- 

                                                                Here, we have first set a while loop that will iterate 3 times as stated earlier. Then we have made a for loop whose value will start from 1 and it will check the condition i<=n(here,n=3) which means the loop will iterate until the loop is executed 3 times. Then an inner is set with a condition j<=n-i(for first iteration n=3 and i=1, for second i=2, third i=3 and so on) for printing the spaces before printing the stars to give the proper shape of a tree. Here, the loop will print spaces until j<=n-i (for the first case it prints 2 spaces as j=3-1=2; for the second case it prints 1 space as j=3-2=1; for third, it will print any space as j=3-3=0).

Then the next ‘for loop’ comes for the printing of stars according to the pattern. Here the condition will be different i.e. k<=(2*i-1) as we need to print stars in ‘odd order’. For first case i=1;k=1; it prints only one star (it should be noted, the spaces are printing at their respective positions before printing stars as per previous ‘for loop’), for second case i=2;k=3, prints 3 stars and for third case i=3;k=5 prints 5 stars.
Now, as the while loop is given so the same will iterate or execute 3 times and thus printing the “head part” of the ‘TREE’.

while(x<=3)
{
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=n-i;j++)
    {
    printf(" ");
    }
    for(k=1;k<=2*i-1;k++)
    {
        printf("*");
    }
  printf("\n");
    }
  x++;
    }

(ii)”STOCK PART”:-
                                  It is a simple part. Here we have for loop with a ‘if-else’ function. The loop is executed a total of 10 times(5 times for the stock part and the rest for the base part).
"If statement" is given to check the condition that the loop is iterated less than 6 times for ‘stock printing’ and inside if statement there is another for loop that will print spaces and then stars with a new line the character at the time of each iteration.
Thus, the ‘stock part’ is printed.

for(i=1;i<=10;i++)
   {
  if(i<6)
  {
     for(j=1;j<=2;j++)
     {
     printf(" ");
     }
     printf("*\n");


(iii)”BASE PART”:-
                                In the previous ‘if statement’ we get the ‘else part’ also, so it is basically for base part printing(“=”). The for loop will execute another 5 times thus completing its total 10 iterations (5 in ‘if’+5 in ‘else’) and printing the’=’ characters . Thus printing the base part.

 for(i=1;i<=10;i++)
   {
  if(i<6)
  {
     for(j=1;j<=2;j++)
     {
     printf(" ");
     }
     printf("*\n");
      }
      else
        printf("=");
   }
}


This is all about printing a “TREE” using ‘C’ Programming. It can also be done in another way (stay tuned). Thank you..............


No comments:

Post a Comment

Can we use pointers without specifying the data type of the pointer?

Void Pointer Yes, we can use pointers without specifying the data type of the pointers. But remember at the end of the programming, wh...