With the use of examples, we will go over how to design various patterns in this article.
The ability to create logic while using the Flow Control Statements is increased by practising Pattern exercises, which are frequently advised by programmers as well as in publications. Additionally, it improves logical reasoning skills. This article will provide beginners and intermediate programmers with a collection of number patterns to practice.
In this code, as we know, we have to print a triangle in the form of numbers 1 to 10. Initially, we have taken k=1, i = rows, j = column, and in this code, we will run two loops, one for each row and another one for the column, so the first loop says for(i = 0; i<=4;i++) the number of rows keeps on increasing one by one in further steps. Now in the second loop for(j = 0; j<i; j++), the number of columns will increase, and both the loops will keep on working till 10.
#include <stdio.h>
int main()
{
{
int i, j;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 4; j++)
printf("%d", i);
printf("\n");
}
return (0);
}
}
Output1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
Square Pattern Explanation
In this code, we want to print a square numeric pattern. Two loops will run here, as you can see in the first loop it’s written for(i=1;i<=4;i++)means each row will carry the same number but it is according to the second loop for(j=1;j<=4;j++) the number will increase.
Introduction of Star pattern in C
In the C programming language, you only need to perform two or three loops to create a star pattern. How many loops you use will depend on the pattern you need to make. One for a row and one for a column are required as the minimal number for a pattern. The first loop is referred to as an outer loop and displays the rows, while the second loop is referred to as an inner loop and displays the columns.
C Program to Print Star Triangle
#include <stdio.h>
int main()
{
{
int i, j, k;
for (i = 1; i <= 5; i++)
{
for (j = 4; j >= i; j--)
{
printf(" ");
}
for (k = 1; k <= (2 i - 1); k++)
{
printf(”“);
}
printf("\n");
}
return (0);
}
}
Output
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Star Triangle Explanation
In this code, we will run an outer loop from 1 to i to iterate through the rows. The loop should be formatted as for(i=1; i<=5; i++) Run an inner loop from I to rows in order to iterate through the columns. The loop should be formatted as for(j=4; j>=i; j--).
Run a second inner loop from 1 to 2 * i - 1 to print the star. The appropriate loop form is for(k=1; k<=(2*i-1); k++).*This loop print has a star inside. Move to the next line, or print a new line, after printing each column in a row.
C Program to Print Inverted Star Pattern
#include <stdio.h>
int main()
{
{
int i, j, k;
for (i = 4; i >= 1; i--)
{
for (j = 4; j > i; j--)
{
printf(" ");
}
for (k = 1; k < (i *2); k++)
{
printf(””);
}
printf("\n");
}
return (0);
}
}
Output* * * * * * *
* * * * *
* * *
*
Inverted Star Pattern Explanation
In this code, we will run an outer loop from 1 to i to iterate through the rows. The loop should be formatted as for(i=4;i>=1;i--). Run an inner loop from I to rows in order to iterate through the columns. The loop should be formatted as for(j=4; j>=i; j--).
Run a second inner loop from 1 to 2 * i - 1 to print the star. The appropriate loop form is for(k=1; k<(i2); k++).*This loop print has a star inside. Move to the next line, or print a new line, after printing each column in a row.
C Program to Print Star Triangle
#include <stdio.h>
int main()
{
{
int i, j;
for (i = 1; i <= 6; i++)
{
for (j = 1; j < i; j++)
{
printf("*");
}
printf("\n");
}
return (0);
}
}
Output*
* *
* * *
* * * *
* * * * *
Star Triangle Explanation
In the above code, the two for loops are used as you can see. here we used i, j, and k as required. on the execution of both loops, stars will be printed. On the different lines. Move to the next line, or print a new line, after printing each column in a row.
C Program to Print Inverted Star Triangle
#include <stdio.h>
int main()
{
{
int i, j;
for (i = 5; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
return (0);
}
}
Output* * * * *
* * * *
* * *
* *
*
Inverted Star Triangle Explanation
In this code, we will run an outer loop from 1 to i to iterate through the rows. The loop should be formatted as for(i=5; i>=1; i- - ). Run an inner loop from I to rows in order to iterate through the columns. The loop should be formatted as for(j=1; j<=i; j++). This loop print has a star inside. Move to the next line, or print a new line, after printing each column in a row.
Introduction of Alphabet Pattern in C
Any alphabet pattern can be printed in C. Here, I'll show how to print the alphabet pattern in C with an explanation.
A set of upper or lowercase alphabets which together form a pattern or geometric shape, such as a square, pyramid, triangle, etc., is known as an alphabet pattern. Controlled loops that are nested are being used to produce these patterns.
C Program to Print Alphabet Pattern
#include <stdio.h>
int main()
{
{
int i, j;
for (i = 1; i <= 6; i++)
{
for (j = 1; j <= i; j++)
{
printf("%c", 'A' + j - 1);
}
printf("\n");
}
return (0);
}
}
OutputA
AB
ABC
ABCD
ABCDE
ABCDEF
Alphabet Pattern Explanation
In this code, we will run an outer loop from 1 to i to iterate through the rows. The loop should be formatted as for(i=1;i<=6;i++). Run an inner loop from I to rows in order to iterate through the columns. The loop should be formatted as for(j=1;j<=i;j++). And using this structure,'A' + j-1 Move to the next line, or print a new line, after printing each column in a row.
C Program to Print Alphabet Pattern
#include <stdio.h>
int main()
{
{
int i, j;
for (i = 1; i <= 6; i++)
{
for (j = 1; j <= i; j++)
{
printf("%c", 'A' - 1 + i);
}
printf("\n");
}
return (0);
}
}
Output
A
BB
CCC
DDDD
EEEEE
FFFFFF
Alphabet Pattern Explanation
In this code, we will run an outer loop from 1 to i to iterate through the rows. The loop should be formatted as for(i=1;i<=6;i++). Run an inner loop from I to rows in order to iterate through the columns. The loop should be formatted as for(j=1;j<=i;j++). And using this structure, 'A' -1+i; Move to the next line, or print a new line, after printing each column in a row.
C Program to Print Inverted Alphabet Program
#include <stdio.h>
int main()
{
{
int i, j;
for (i = 1; i <= 6; i++)
{
for (j = 6; j >= i; j--)
{
printf("%c", 'A' - 1 + i);
}
printf("\n");
}
return (0);
}
}
Output
AAAAAA
BBBBB
CCCC
DDD
EE
F
Inverted Alphabet Program Explanation
In this code, we will run an outer loop from 1 to i to iterate through the rows. The loop should be formatted as for(i=1; i<=6; i++). Run an inner loop from I to rows in order to iterate through the columns. The loop should be formatted as for(j=6;j<=i;j - -). And using this structure, 'A' -1+i Move to the next line, or print a new line, after printing each column in a row.
Conclusion
Pyramid patterns come in different types. The most crucial thing is to understand the structure of numbers and whitespaces. Once you understand the pattern, writing code in C or any other programming language will come naturally to you.
References:
https://www.youtube.com/watch?v=v3gcXj9k4k8https://www.interviewbit.com/c-interview-questions/