Contact Learn C
Copy Program Share Program

Program 20:To Print Diamond for given number of rows

Program 20:
#include<stdio.h>
main()
{
 int i,j,k,count,number;
 printf("Enter number of rows\n");
 scanf("%d",&number);
 count=number-1;
 for(i=1;i<=2*(number)-1;i+=2)//first for loop
 {
  for(k=1;k<=count;k++)
  {
   printf(" ");
  }
  count--;
  for(j=1;j<=i;j++)       
  {       
   printf("*");       
  }       
  printf("\n");       
 }  //end of first for loop      
 count=1;        
 for(i=2*(number)-1;i>=1;i-=2)//second for loop
 {
   if(i!=(2*(number)-1))
   {
     for(k=1;k<=count;k++)      
     {      
      printf(" ");      
     }      
     count++;      
     for(j=i;j>=1;j--)
     {
       printf("*");
     }
     printf("\n");
   }
 }//end of second for loop
}
Explanation:
  1. The program starts with initializing
    • i,j,khelping variable
    • number → to store number of rows
    • count → to store number of spaces
  2. There are two for loops. First one is used to print a normal triangle and second is used to print reverse triangle
  3. Say in the below example we want 10 rows to be printed.First for loop prints 10 rows of normal triangle and second will do the same in reverse.
  4. Coming to 1st for loop
    • for(i=1;i<=2*(number)-1;i+=2)
      It will iterate the loop from i=1 to 19 with increment of 2 each time so that we could use i value for stars to be printed in odd number times(like 1,3,5,7...19).
    • for(k=1;k<=count;k++)
      {
      printf(" ");
      }
      count--;
      This loop will print the spaces for each iteration.
    • As if you see in the output below:for 1st star it needs 9 spaces from the left end and it should go on decreasing for every loop which is why we used count--
    • for(j=1;j<=i;j++)    
      {    
      printf("*");    
      }    
      printf("\n");    
      }  //end of first for loop
      This is used to print stars with i number of times.For example if i = 19 in that iteration it will print stars that number of times.It will print stars from 1 to i as the stars go on increasing in upper triangle
    • So till now we got upper triangle.
  5. Coming to 2nd for loop
    • for(i=2*(number)-1;i>=1;i-=2)//second for loop
      {
      If you see this this is same as above one but here we need to make it reverse so the loop will iterate from i=19 to 1 by decrementing 2 each time (like 19,17,15...1)
    • if(i!=(2*(number)-1))
      {
      If you observe the output below you will find 19 stars at the end of first triangle which is done by first loop but for second loop we should not print 19 stars or 2(number)-1 th time. So we are skipping that iteration.
    • for(k=1;k<=count;k++)    
      {   
       printf(" ");   
       }    
      count++;
      We made count=1 at the end of first forloop.Beacause in the upper triangle we need spaces which decreases gradually for every loop from left end.But in the lower triangle the spaces should increase for every iteration of second loop.
    • for(j=i;j>=1;j- -)
      {
      printf("*");
      }
      This is used to print stars with i number of times.For example if i = 15 in that iteration it will print stars that number of times.Here the stars goes on decreasing as we go lower which is why we are printing the stars from i to 1 in each iteration in lower triangle.
Output:
Print Diamond for given number of rows

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide