Patterns in C Programming: Top 10 Pattern Printing Examples

A basic C programming exercise that helps with loops, conditional statements, and nested structures is pattern printing. Programmers can enhance their logical thinking and problem-solving abilities by practicing various problem-solving patterns. With code snippets and explanations to help both novice and intermediate learners, this blog explores the top 10 pattern printing examples in C.

Blogging Illustration

1. Right-Angled Triangle Pattern

This pattern uses asterisks to show a right-angled triangle.

int main() {
    int i, j;
    for(i = 1; i <= 5; i++) {
        for(j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

The inner loop prints the asterisks that correspond to the current row number, while the outer loop manages the number of rows.

One of the most basic and widely used patterns in C programming is the right-angled triangle pattern. A sequence of asterisks (*) must be printed so that each row has one extra asterisk over the one before it. Beginners can learn the fundamentals of nested loops and how to manage the number of iterations with the aid of this pattern.

  • To deal with the number of rows, use an outer loop.
  • The number of columns grows with each row, so handle it with an inner loop.
  • Every time the inner loop iterates, print an asterisk.
  • Proceed to the following line once the inner loop is finished.

2. Inverted Right-Angled Triangle Pattern

The right-angled triangle pattern is reversed in this pattern.

int main() {
    int i, j;
    for(i = 5; i >= 1; i--) {
        for(j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

The inner loop prints the decreasing number of asterisks while the outer loop decreases the number of rows.

The right-angled triangle pattern is reversed in the inverted right-angled triangle pattern. The first row has the most asterisks possible, and each succeeding row has one fewer. The idea of decrementing loops is reinforced by this pattern.

  • To deal with the number of rows, use an outer loop.
  • Use an inner loop to handle the number of columns, which decreases with each row.
  • Every time the inner loop iterates, print an asterisk.
  • Proceed to the following line once the inner loop is finished.

3. Pyramid Pattern

This pattern uses asterisks to create a symmetrical pyramid.

int main() {
    int i, j, space;
    for(i = 1; i <= 5; i++) {
        for(space = 1; space <= 5 - i; space++) {
            printf(" ");
        }
        for(j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

The pyramid is centered by adding spaces in the first inner loop, and the asterisks are printed in the second inner loop.

A symmetrical arrangement of asterisks is produced in the center of the screen by the pyramid pattern. To get the desired shape, asterisks and spaces must be handled carefully. This pattern presents the idea of using spaces to align output.

  • To deal with the number of rows, use an outer loop.
  • Print the spaces before the asterisks using an inner loop. With every row, there are fewer spaces.
  • To print asterisks, use an additional inner loop. In each row, there are two more asterisks.
  • Proceed to the following line after printing the asterisks and spaces.

4. Inverted Pyramid Pattern

An inverted version of the pyramid pattern.

int main() {
    int i, j, space;
    for(i = 5; i >= 1; i--) {
        for(space = 1; space <= 5 - i; space++) {
            printf(" ");
        }
        for(j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

Though the outer loop decrements to invert the pyramid, the pattern is similar.

The pyramid pattern is reversed in the inverted pyramid pattern. As the number of leading spaces rises, it begins with the most asterisks possible in the first row and drops by two in each succeeding row. This pattern highlights how crucial it is to manage both asterisks and spaces.

  • To deal with the number of rows, use an outer loop.
  • Print the spaces before the asterisks using an inner loop. With every row, there are more spaces.
  • To print asterisks, use an additional inner loop. In each row, there are two fewer asterisks.
  • Proceed to the following line after printing the asterisks and spaces.

5. Diamond Pattern

Creates a diamond by combining the pyramid and the inverted pyramid.

int main() {
    int i, j, space;
    // Upper half
    for(i = 1; i <= 5; i++) {
        for(space = 1; space <= 5 - i; space++) {
            printf(" ");
        }
        for(j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }
        printf("\n");
    }
    // Lower half
    for(i = 4; i >= 1; i--) {
        for(space = 1; space <= 5 - i; space++) {
            printf(" ");
        }
        for(j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

The diamond's upper and lower halves are made by the first and second loops, respectively.

The diamond pattern creates a symmetrical diamond shape by combining the pyramid and inverted pyramid patterns. To keep this pattern symmetrical, the quantity of asterisks and spaces must be carefully managed.

  • The top half of the diamond (pyramid) is made using an outer loop.
  • Print asterisks and spaces for every row using nested loops.
  • The lower half of the diamond (inverted pyramid) is made using a second outer loop.
  • Print asterisks and spaces for every row using nested loops.

To preserve symmetry, make sure that the quantity of asterisks and spaces is suitably changed.

6. Number Pyramid Pattern

A pyramid pattern with numbers in place of asterisks.

int main() {
    int i, j, space;
    for(i = 1; i <= 5; i++) {
        for(space = 1; space <= 5 - i; space++) {
            printf(" ");
        }
        for(j = 1; j <= i; j++) {
            printf("%d", j);
        }
        for(j = i - 1; j >= 1; j--) {
            printf("%d", j);
        }
        printf("\n");
    }
    return 0;
}

The pattern creates a symmetrical number pyramid by increasing the numbers to the midpoint and then decreasing them.

Numbers are shown in a pyramid shape using the number pyramid pattern, rising to a peak and then falling. This pattern aids in comprehending how to work with numbers inside loops.

  • To deal with the number of rows, use an outer loop.
  • Print the spaces before the numbers using an inner loop. With every row, there are fewer spaces.
  • To print numbers in ascending order up to the current row number, use another inner loop
  • To print descending numbers back to 1, utilize a third inner loop.
  • Proceed to the following line after printing the numbers and spaces.

7. Floyd’s Triangle

Sequential numbers in a right-angled triangle.

int main() {
    int i, j, num = 1;
    for(i = 1; i <= 5; i++) {
        for(j = 1; j <= i; j++) {
            printf("%d ", num);
            num++;
        }
        printf("\n");
    }
    return 0;
}

A growing number of consecutive numbers is contained in each row.

Floyd's Triangle is a triangular array of natural numbers oriented at a right angle. It is created by adding consecutive numbers, beginning with 1, to the rows. This pattern helps practice nested loops and sequences of numbers.

  • Set the initial value of a counter variable to 1.
  • To deal with the number of rows, use an outer loop.
  • Print the numbers in each row using an inner loop. Each row has the same number of elements as the row number.
  • Print the counter and increment it in each inner loop iteration.
  • Proceed to the following line once the inner loop is finished.

8. Pascal’s Triangle

Each number in a triangle is equal to the sum of the two numbers that are directly above it.

int main() {
    int i, j, space, coef;
    for(i = 0; i < 5; i++) {
        for(space = 1; space <= 5 - i; space++) {
            printf(" ");
        }
        for(j = 0; j <= i; j++) {
            if(j == 0 || i == 0)
                coef = 1;
            else
                coef = coef * (i - j + 1) / j;
            printf("%d ", coef);
        }
        printf("\n");
    }
    return 0;
}

Computes each number using the binomial coefficient formula.

A triangular array of binomial coefficients is known as Pascal's Triangle. Every number is equal to the sum of the two numbers that are right above it. The idea of mathematical calculations inside loops is introduced by this pattern.

  • To deal with the number of rows, use an outer loop.
  • Print the spaces before the numbers using an inner loop. With every row, there are fewer spaces.
  • Utilize an additional inner loop to compute and output the binomial coefficients by applying the following formula: C(n, k) = n! / (k! * (n-k)!)
  • Proceed to the following line after printing the numbers and spaces.

9. Hollow Square Pattern

A hollow center in a square pattern.

int main() {
    int i, j;
    for(i = 1; i <= 5; i++) {
        for(j = 1; j <= 5; j++) {
            if(i == 1 || i == 5 || j == 1 || j == 5)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Prints asterisks on the interior spaces and borders.

A square border composed of asterisks surrounds the hollow square pattern, while the interior is left empty. This pattern facilitates comprehension of conditional statements in loops.

  • To deal with the number of rows, use an outer loop.
  • To deal with the quantity of columns, utilize an inner loop.
  • Verify whether the current position is on the border (first row, last row, first column, or last column) in each iteration of the inner loop. Print an asterisk if it is; if not, print a space.
  • Proceed to the following line once the inner loop is finished.

10. Alphabet Pyramid Pattern

An alphabet-based pyramid pattern.

int main() {
    int i, j, space;
    char ch;
    for(i = 1; i <= 5; i++) {
        for(space = 1; space <= 5 - i; space++) {
            printf(" ");
        }
        for(j = 1, ch = 'A'; j <= i; j++, ch++) {
            printf("%c", ch);
        }
        for(j = i - 1, ch = ch - 2; j >= 1; j--, ch--) {
            printf("%c", ch);
        }
        printf("\n");
    }
    return 0;
}

Creates a symmetrical pyramid by building the alphabet up to the halfway point and then reversing it.

Letters are arranged in a pyramid shape in the alphabet pyramid pattern, rising to a peak and then falling. This pattern facilitates comprehension of ASCII values and character manipulation.

  • To deal with the number of rows, use an outer loop.
  • Print the spaces before the letters using an inner loop. With every row, there are fewer spaces.
  • To print ascending characters up to the current row number, use an additional inner loop.
  • To print descending characters back to 'A', utilize a third inner loop.
  • Proceed to the following line after printing characters and spaces.

Gaining proficiency in pattern printing in C is a prerequisite for learning programming. These illustrations provide a solid foundation for understanding conditionals, loops, and nested structures. Enrolling in a C programming course in Noida can be very helpful for individuals who want to learn more and get practical experience. These classes provide well-organized learning pathways, knowledgeable mentoring, and real-world projects to help you grasp the material.

Placed Students

Our Clients

Partners

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses