Pattern programs are nothing but patterns consisting of numbers, alphabets, or symbols in a particular form. Using for loop conditions, these pattern questions can be easily solved.
- Pattern programs are the best means to study the working of loop structures in general purpose programming language. It helps beginners visualise how every iteration in a loop works.
- It contains nested loops where the outer loop controls the number of rows, and the inner loop controls the data in each row containing the contents to be printed.
Pyramid Pattern Programs
Using stars:
#include <stdio.h>
int main()
{
int i, j,n;
scanf(“%d”,&n); //Input number of rows
for(i = 0; i < n; i++){
for(j = 0; j <= i; j++){
printf(“*”);
}
printf(“\n”);
}
return 0;
}
//The output for 5 rows will be:
/*********************************************************************************************
*
* *
* * *
* * * *
* * * * *
********************************************************************************************* /
Using numbers:
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
for (int i = 1; i <= n; i++) {
//P1 part printing
//first print spaces n-i times
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
//then print elements starting from i to 2*i-1, which can be observed in each row of the pattern.
for (int j = i; j < 2 * i; j++) {
printf("%d ", j);
}
//P2 part printing
//each row starts with 2*(i-1) and then decrease i-1 times
int ele = 2 * (i - 1);
for (int j = 1; j <= i - 1; j++) {
printf("%d ", ele--);
}
//finish each row and then print a new line
printf("\n");
}
return 0;
}
/****************
OUTPUT:
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
***************** /
Palindrome Pyramid:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,k,l,n;
printf(“Enter the number of rows : “);
scanf(“%d”,&n);
for(i = 1; i <= n; i++){
for(k = 1; k <= i; k++){
printf(“%d “,k);
}
for(l = i-1; l >= 1; l–){
printf(“%d “,l);
}
printf(“\n”);
}
return 0;
}
/******
OUTPUT:
Enter the number of rows : 5
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
****** /
Pascal Triangle:
Pascal’s triangle, each element is equal to the sum of the two numbers immediately above it.
#include<stdio.h>
int main()
{
int rows, coef = 1, space, i, j;
printf("\nEnter the number of rows : ");
scanf("%d",&rows);
printf("\n");
for(i=0; i<rows; i++)
{
for(space=1; space <= rows-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\n");
}
return 0;
}
/*****
OUTPUT:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
***** /
Floyd’s Triangle:
The Floyd’s triangle is a right-angled triangle that contains consecutive natural numbers.
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int num = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; ++j)
{
printf("%d ", num);
++num;
}
printf("\n");
}
return 0;
}
/***********
OUTPUT:
1
2 3
4 5 6
7 8 9 10
***********/
For queries regarding the content, please drop a mail to loop@cumminscollege.in Links for in-depth studying and practice problems are sent via mail to students of the college on the date of publishing of the blog by Loop Club. The content is a part of DSA Bulletins started by Team Loop, to provide thorough knowledge and facilitate the learning of new topics of DSA. We are a group of students who are passionate about CP and DSA and want to create awareness and help students in the above topics.