Blog

  • Half Hourglass Number pattern in C

    In this tutorial, we will write a C Program to display half hourglass patterns using numbers. Before that, you may go through the following topic in C.

    Half Hourglass number pattern in C

    1. Left half hourglass number pattern

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

    Output:

    Enter the number of rows: 5
    12345
     2345
      345
       45
        5
       45
      345
     2345
    12345

    2. Right half hourglass number pattern

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = rows; i >= 1; i--)
      {
        for (j = 1; j <= i; j++)
          printf("%d", j);
    
        printf("\n");
      }
    
      for (i = 2; i <= rows; i++)
      {
        for (j = 1; j <= i; j++)
          printf("%d", j);
    
        printf("\n");
      }
    
      return 0;
    }

    Output:

    Enter the no. of rows: 5
    12345
    1234
    123
    12
    1
    12
    123
    1234
    12345


  • Left Half Pyramids in C using Numbers

    In this tutorial, we will learn and write codes for various half pyramids number patterns in C programming language. However, in this tutorial, we will create a numeric pattern in C using for loop. So you may go through the following topic in C.

    • for loop in C : loops plays a key role to achieve various patterns in any programming language. Mastering patterns means having excellent grasp at loops.

    Left Half Pyramids in C using Numbers

    We will look at the following topic of pyramid patterns in C. Each of the programs is consists of different number patterns.

    • Left half pyramid
    • Inverted left half pyramid

    Left half pyramid pattern of numbers in C

    Pattern 1:

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

    Output:

    Enter the no. of rows: 5
             1
           1 2
         1 2 3
       1 2 3 4
     1 2 3 4 5

    Inverted left half pyramid patterns of Numbers in C

    Pattern 1:

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

    Output:

    Enter the no. of rows: 5
       1 2 3 4 5
         2 3 4 5
           3 4 5
             4 5
               5

  • Half Pyramid Number Pattern in C

    In this tutorial, we will learn and write codes for various half pyramids number patterns in C programming language. However, in this tutorial, we will create a numeric pattern in C using for loop. So you may go through the following topic in C.

    • for loop in C : loops plays a key role to achieve various patterns in any programming language. Mastering patterns means having excellent grasp at loops.

    Half Pyramid Number Pattern in C

    We will look at the following topic of pyramid patterns in C. Each of the programs is consists of different number patterns.

    • Right half pyramid
    • Inverted right half pyramid

    Right half Pyramid Pattern

    Pattern 1: Also called the right triangle pattern.

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = 1; i <= rows; ++i)
      {
        for (j = 1; j <= i; ++j)
        {
          printf("%d ", j);
        }
    
        printf("\n");
      }
    
      return 0;
    }

    Enter the no. of rows: 5
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5


    Pattern 2:

    #include <stdio.h>
    
    int main()
    {
      int rows, count = 1;
    
      printf("Enter the number of rows: ");
      scanf("%d", &rows);
    
      for (int i = 1; i <= rows; i++)
      {
        for (int j = 1; j <= i; j++)
        {
          printf("%d ", count);
          ++count;
        }
    
        printf("\n");
      }
    
      return 0;
    }

    Enter the number of rows: 4
    1
    2 3
    4 5 6
    7 8 9 10


    Pattern 3: Pattern with a binary number in C

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the number of rows: ");
      scanf("%d", &rows);
    
      for (i = 1; i <= rows; i++)
      {
        for (j = 1; j <= i; j++)
        {
          if (j % 2 == 0)
            printf("0");
          else
            printf("1");
        }
    
        printf("\n");
      }
    
      return 0;
    }

    Enter the number of rows: 6
    1
    10
    101
    1010
    10101
    101010


    Pattern 4: Right-angled triangle pattern

    The patterns have a similar number in each row. It can also be called half pyramid number patterns in C.

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = 1; i <= rows; i++)
      {
        for (j = 1; j <= i; j++)
          printf("%d ", i);
    
        printf("\n");
      }
    
      return 0;
    }

    Enter the no. of rows: 5
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5


    Pattern 5:

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

    Enter the no. of rows: 10
    1
    2 4
    3 6 9
    4 8 12 16
    5 10 15 20 25
    6 12 18 24 30 36
    7 14 21 28 35 42 49
    8 16 24 32 40 48 56 64
    9 18 27 36 45 54 63 72 81
    10 20 30 40 50 60 70 80 90 100


    Pattern 6:

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = rows; i >= 1; i--)
      {
        for (j = rows; j >= i; j--)
          printf("%d ", j);
    
        printf("\n");
      }
    
      return 0;
    }

    Enter the no. of rows: 5
    5
    5 4
    5 4 3
    5 4 3 2
    5 4 3 2 1


    Pattern 7:

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = rows; i >= 1; i--)
      {
        for (j = rows; j >= i; j--)
          printf("%d ", i);
    
        printf("\n");
      }
    
      return 0;
    }

    Enter the no. of rows: 5
    5
    4 4
    3 3 3
    2 2 2 2
    1 1 1 1 1


    Inverted right half Pyramid Pattern

    Pattern 1:

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

    Enter the no. of rows: 6
    1 2 3 4 5 6
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1


    Pattern 2:

    Half inverted right-angled triangle with decreasing order.

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = rows; i >= 1; i--)
      {
        for (j = i; j >= 1; j--)
        {
          printf("%d ", j);
        }
    
        printf("\n");
      }
    
      return 0;
    }

    Enter the no. of rows: 5
    5 4 3 2 1
    4 3 2 1
    3 2 1
    2 1
    1


    Pattern 3:

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

    Enter the no. of rows: 5
    5 5 5 5 5
    4 4 4 4
    3 3 3
    2 2
    1


    Pattern 4:

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

    Enter the no. of rows: 5
    5 4 3 2 1
    5 4 3 2
    5 4 3
    5 4
    5


    Pattern 5:

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = 1; i <= rows; i++)
      {
        for (j = i; j <= rows; j++)
          printf("%d ", j);
    
        printf("\n");
      }
    
      return 0;
    }

    Enter the no. of rows: 5
    1 2 3 4 5
    2 3 4 5
    3 4 5
    4 5
    5


    Pattern 6:

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = 1; i <= rows; i++)
      {
        for (j = i; j <= rows; j++)
          printf("%d ", i);
    
        printf("\n");
      }
    
      return 0;
    }

    Enter the no. of rows: 5
    1 1 1 1 1
    2 2 2 2
    3 3 3
    4 4
    5


  • Full Pyramid Number Pattern in C

    In this tutorial, we will write a C Program to display Pyramid patterns using numbers. Before that, you may go through the following topic in C.

    Full Pyramid Number Pattern in C

    1. Full pyramid with mirror pattern of numbers.

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows, k = 0, count1 = 0, count2 = 0;
    
      printf("Enter the no of rows: ");
      scanf("%d", &rows);
    
      for (i = 1; i <= rows; ++i)
      {
        for (j = 1; j <= rows - i; ++j)
        {
          printf("  ");
          ++count1;
        }
    
        while (k != 2 *i - 1)
        {
          if (count1 <= rows - 1)
          {
            printf("%d ", i + k);
            ++count1;
          }
          else
          {
            ++count2;
            printf("%d ", (i + k - 2 *count2));
          }
    
          ++k;
        }
    
        count2 = count1 = k = 0;
        printf("\n");
      }
    
      return 0;
    }
    Enter the no of rows: 6
              1 
            2 3 2 
          3 4 5 4 3 
        4 5 6 7 6 5 4 
      5 6 7 8 9 8 7 6 5 
    6 7 8 9 10 11 10 9 8 7 6 

    2. Full pyramid with repeating pattern of numbers.

    #include <stdio.h>
    
    int main()
    {
      int rows, count = 1;
    
      printf("Enter the number of rows: ");
      scanf("%d", &rows);
    
      for (int i = rows; i > 0; i--)
      {
        for (int j = 1; j <= i; j++)
          printf(" ");
    
        for (int j = 1; j <= count; j++)
          printf("%d ", count);
    
        printf("\n");
    
        count++;
      }
    
      return 0;
    }
    Enter the number of rows: 6
          1 
         2 2 
        3 3 3 
       4 4 4 4 
      5 5 5 5 5 
     6 6 6 6 6 6 

    3. Full pyramid pattern of numbers.

    #include <stdio.h>
    
    int main()
    {
      int rows, count = 1;
    
      printf("Enter the number of rows: ");
      scanf("%d", &rows);
    
      for (int i = rows; i > 0; i--)
      {
        for (int j = 1; j <= i; j++)
          printf(" ");
    
        for (int j = 1; j <= count; j++)
          printf("%d ", j);
    
        printf("\n");
    
        count++;
      }
    
      return 0;
    }
    Enter the number of rows: 6
          1 
         1 2 
        1 2 3 
       1 2 3 4 
      1 2 3 4 5 
     1 2 3 4 5 6

  • Hourglass number Pattern in C

    In this tutorial, we will write a C Program to display full hourglass patterns using numbers. Before that, you may go through the following topic in C.

    Hourglass number Pattern in C

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

    Output:

    Enter the number of rows: 6
    1 2 3 4 5 6 
     2 3 4 5 6 
      3 4 5 6 
       4 5 6 
        5 6 
         6 
        5 6 
       4 5 6 
      3 4 5 6 
     2 3 4 5 6 
    1 2 3 4 5 6

  • Inverted hollow Triangle Star pattern in C

    In this tutorial, we will write a C program to display a hollow inverted triangle star pattern. Before that, you may go through the following topics in C.

    Inverted hollow Triangle Star pattern in C

    It is also called Hollow Pyramid Star Pattern in C.

    #include <stdio.h>
    
    int main()
    {
      int i, j, k, m = 1, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = rows; i >= 1; i--)
      {
        for (j = 1; j < m; j++)
          printf(" ");
    
        for (k = 1; k <= 2 *i - 1; k++)
        {
          if (k == 1 || k == 2 *i - 1 || i == rows)
            printf("*");
          else
            printf(" ");
        }
        m++;
    
        printf("\n");
      }
    
      return 0;
    }

    Output:

    inverted hollow triangle pattern in c

  • Diagonal Number Pattern in C

    In this tutorial, we will learn and write a program in C to display the diagonal number pattern. So you may go through the following topic in C.

    Diagonal Number Pattern in C

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = 1; i <= rows; i++)
      {
        for (j = i; j < rows; j++)
          printf(" ");
    
        printf(" %d\n", i);
      }
    
      return 0;
    }

    Output:

    Diagonal Number Pattern in C 1

    #include <stdio.h>
    
    int main()
    {
      int i, j, k = 1, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = 1; i <= (2 *rows) - 1; i++)
      {
        for (j = 1; j <= (2 *rows) - 1; j++)
        {
          if (i == j || i + j == 2 *rows)
            printf(" %d", k);
          else
            printf("  ");
        }
    
        if (i < rows)
          k++;
        else
          k--;
    
        printf("\n");
      }
    
      return 0;
    }

    Output:

    Diagonal Number Pattern in C 2

  • Diagonal Star Pattern in C

    In this tutorial, we will write a program in C to display the diagonal star pattern. So you may go through the following topic in C.

    Diagonal Star Pattern in C

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      int m = (2 *rows) - 1;
    
      for (i = 1; i <= m; i++)
      {
        for (j = 1; j <= m; j++)
        {
          if (i == j || j == (m - i + 1))
            printf("*");
          else
            printf(" ");
        }
        printf("\n");
      }
    
      return 0;
    }

    Output:

    Diagonal Star Pattern in C

  • C Program to print Half Hourglass Star pattern

    In this tutorial, we will write a program to print half hourglass pattern in C. Before that, you may go through the following topic in C.

    1. Right half hourglass pattern in C

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

    Enter the no. of rows: 5
    * * * * *
    * * * *
    * * *
    * *
    *
    *
    * *
    * * *
    * * * *
    * * * * *


    2. Left half hourglass pattern in C

    #include <stdio.h>
    
    int main()
    {
      int i, j, k, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      printf("Output: \n\n");
      //upper half
      for (i = 1; i <= rows; i++)
      {
        for (k = 1; k < i; k++)
          printf(" ");
    
        for (j = i; j <= rows; j++)
          printf("*");
    
        printf("\n");
      }
    
      //lower half
      for (i = rows - 1; i >= 1; i--)
      {
        for (k = 1; k < i; k++)
          printf(" ");
    
        for (j = i; j <= rows; j++)
          printf("*");
    
        printf("\n");
      }
    
      return 0;
    }

    Output:

    java half hourglass pattern

  • Half Diamond Star Pattern in C

    We will write two different patterns diamond shape programs in C. So you may go through the following topic in C first.

    1. Right half diamond pattern in C

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

    Enter the no. of rows: 5
    *
    * *
    * * *
    * * * *
    * * * * *
    * * * *
    * * *
    * *
    *


    2. Left half diamond pattern in C

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