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.
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.
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.
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.
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.
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.
To preserve symmetry, make sure that the quantity of asterisks and spaces is suitably changed.
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.
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.
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.
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.
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.
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.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR