Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Print symmetric double triangle pattern in C language
In C, printing a symmetric double triangle pattern involves creating a diamond-like structure using alternating X and O characters. This pattern consists of three main sections: upper half, middle line, and lower half.
Syntax
void printPattern(int n); void printX(int count); void printO(int count);
Pattern Structure
The symmetric double triangle pattern contains three parts −
- Upper half: n-1 lines for odd n or n-2 lines for even n
- Middle line: 1 line for odd n or 3 lines for even n
- Lower half: n-1 lines for odd n or n-2 lines for even n
Example: Complete Implementation
Here's a complete program that generates the symmetric double triangle pattern −
#include <stdio.h>
/* Function to print alternating x o starting with x */
void printX(int n) {
for (int i = 1; i <= n; i++) {
if (i % 2 != 0)
printf("X ");
else
printf("O ");
}
}
/* Function to print alternating o x starting with o */
void printO(int n) {
for (int i = 1; i <= n; i++) {
if (i % 2 != 0)
printf("O ");
else
printf("X ");
}
}
/* Function to print the complete pattern */
void printPattern(int n) {
int x = n;
int i, j;
/* Adjust for even numbers */
if (n % 2 == 0)
x = x - 1;
/* Upper half - number of spaces and characters */
int p = n - 1; /* spaces */
int s = 1; /* characters */
/* Print double lines in each iteration for upper half */
for (i = 1; i <= (x - 1) / 2; i++) {
/* First line of the pair */
for (j = 1; j <= p; j++) {
printf(" ");
}
if (i % 2 != 0)
printX(s);
else
printO(s);
printf("
");
/* Second line of the pair */
p++;
for (j = 1; j <= p; j++)
printf(" ");
if (i % 2 != 0)
printX(s);
else
printO(s);
printf("
");
p--;
s++;
}
/* Extra upper middle for even numbers */
if (n % 2 == 0) {
for (i = 1; i <= p; i++)
printf(" ");
if (n % 4 != 0)
printX(n / 2);
else
printO(n / 2);
printf("
");
}
/* Middle line */
if (n % 2 != 0) {
printX(n);
} else {
if (n % 4 != 0) {
printX(n / 2);
printX(n / 2);
} else {
printX(n / 2);
printO(n / 2);
}
}
printf("
");
/* Extra lower middle for even numbers */
if (n % 2 == 0) {
printf(" ");
printX(n / 2);
printf("
");
}
/* Lower half */
p = 1;
if (n % 2 == 0) {
x--;
p = 2;
}
int q = x / 2;
for (i = 1; i <= x; i++) {
for (j = 1; j <= p; j++)
printf(" ");
printX(q);
if (i % 2 == 0)
q--;
printf("
");
p++;
}
}
int main() {
int n = 5;
printf("Symmetric Double Triangle Pattern for n = %d:
", n);
printPattern(n);
return 0;
}
Symmetric Double Triangle Pattern for n = 5:
X
X
O X
O X
X O X O X
X O
X O
X
X
How It Works
The algorithm divides the pattern into three sections −
- Upper half: Creates pairs of lines with increasing indentation
- Middle section: Prints the widest line(s) with full pattern
- Lower half: Creates single lines with decreasing character count
Conclusion
The symmetric double triangle pattern demonstrates complex nested loop logic and conditional formatting. The key is managing spaces, character alternation, and different behaviors for odd and even input values.
Advertisements
