BlogsDope image BlogsDope

Printing of common patterns using loop in C

Aug. 14, 2017 LOOP C C++ 9063

Printing of patterns using loops is a common question asked to a beginner and is a good practice to make strong grip over loops. This post contains the codes of various patterns. Read the comments in the code to understand the logic.

*
**
***
****
*****

*****
****
***
**
*

    *
   ***
  *****
 *******
*********

*   *
 * * 
  *  
 * * 
*   *

*****
   * 
  *  
 *   
*****

*****
 *   
  *  
   * 
*****

*****
*    *
*    *
*    *
*****

*****
*****
*****
*****
*****

*****
 *****
  *****
   *****
    *****

1
12
123
1234
12345
1234
123
12
1

//Right angled traingle upward
#include <stdio.h>
int main()
{
    int height, i, j, k;
    printf("Enter height\n");
    scanf("%d",&height);

    //number of lines is equal to height
    for(i=1; i<=height; i++)
    {
        //number of stars in each line is
        //equal to the line number
        for(j=1; j<=i; j++)
        {
            printf("*");
        }
        //new line after each line
        printf("\n");
    }
    return 0;
}
//Right angled triangle downward
#include <stdio.h>
int main()
{
    int height, i, j, k;
    printf("Enter height\n");
    scanf("%d",&height);
    for(i=height; i>=1; i--)
    {
        //number of stars in each line is
        //equal to i
        for(j=1; j<=i; j++)
        {
            printf("*");
        }
        //new line after each line
        printf("\n");
    }
    return 0;
}
//Triangle
#include <stdio.h>
int main()
{
    int height, i, j, k;
    printf("Enter height\n");
    scanf("%d",&height);
    //Number of lines is equal to height
    for(i=1; i<=height; i++)
    {
        //Number of spaces in each line is
        //equal to height – line number
        for(j=1; j<=height-i;j++)
        {
            printf(" ");
        }
        //number of stars in each line is equal to
        //(2*line number) – 1
        for(j=1; j<=(2*i)-1; j++)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}
//cross
#include <stdio.h>
int main()
{
    int height, i, j, k;
    printf("Enter height\n");
    scanf("%d",&height);
    for(i=1; i<=height; i++)
   {
     for(j=1; j<=height; j++)
     {
            //in a cross,
            //star will be printed if
            //i is j – digonal line
            //or height-j+1 – another digonal line
           if(i==j || height-j+1==i)
               printf("*");
           else
               printf(" ");
     }
     printf("\n");
   }
    return 0;
}
//z
#include <stdio.h>
int main()
{
    int height, i, j, k;
    printf("Enter height\n");
    scanf("%d",&height);
    for(i=1; i<=height; i++)
   {
     for(j=height; j>=1; j--)
     {
            //i=j – to print one digonal line
            //i==1 – First line will contain only stars
            //i == height – Last line will be of stars only
       if(i==1 || i==height || i==j)
         printf("*");
       else
         printf(" ");
     }
     printf("\n");
    }
    return 0;
}
//reverse z
#include <stdio.h>
int main()
{
    int height, i, j, k;
    printf("Enter height\n");
    scanf("%d",&height);
    for(i=1; i<=height; i++)
   {
     for(j=1; j<=height; j++)
     {
       if(i==1 || i==height || i==j)
         printf("*");
       else
         printf(" ");
     }
     printf("\n");
   }
    return 0;
}
//hollow square
#include <stdio.h>
int main()
{
    int height, i, j, k;
    printf("Enter height\n");
    scanf("%d",&height);
    for(i=1; i<=height; i++)
    {
        for(j=1; j<=height; j++)
        {
            //stars will be printed at top, bottom and side edges
            //j==1 for left side edge
            //j==height for right side edge
            //i==1 for top line
            //i==height for last line
            if(j==1 || j==height || i==1 || i==height)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}
//solid square
#include <stdio.h>
int main()
{
    int height, i, j, k;
    printf("Enter height\n");
    scanf("%d",&height);
    for(i=1; i<=height; i++)
   {
     for(j=1; j<=height; j++)
     {
          printf("*");
     }
     printf("\n");
   }
    return 0;
}
//rhombus
#include <stdio.h>
int main()
{
    int height, i, j, k;
    printf("Enter height\n");
    scanf("%d",&height);
    for(i=1; i<=height; i++)
   {
     for(k=1; k<=i-1;k++)
     {
       printf(" ");
     }

     for(j=1; j<=height; j++)
     {
       printf("*");
     }
     printf("\n");
   }
    return 0;
}
#include <stdio.h>
int main()
{
    int height, i, j, k;
    printf("Enter height\n");
    scanf("%d",&height);
    for(i=1; i<=height; i++)
   {
     for(j=1; j<=i; j++)
     {
       printf("%d",j);
     }
     printf("\n");
   }
     for(i=height-1; i>=1; i--)
   {
     for(j=1; j<=i; j++)
     {
       printf("%d",j);
     }
     printf("\n");
   }
    return 0;
}

Liked the post?
Developer and founder of CodesDope.
Editor's Picks
2 COMMENTS

Please login to view or add comment(s).