Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! ๐ ๐นLearn advanced coding techniques in C& C++ ๐นMaster Linux internals & shell scripting ๐นDeep dive into Data Structures & Algorithms ๐นExplore Embedded Systems & Microcontrollers (8051,UART, RTOS) ๐นGet hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
Hourglass Pattern in C: Star, Number, Alphabet & Binary Patterns with Output
⭐ C Program to Print Inverted and Right-Side Up Binary Triangle Diamond Pattern
#include<stdio.h>
int main()
{
int num;
printf("Enter the number:\n");
scanf("%d",&num);
for(int i=num;i>=1;i--)
{
for(int k=num;k>=i;k--)
{
printf(" ");
}
for(int j=1;j<=i;j++)
{
printf("%d ",j%2);
}
printf("\n");
}
for(int i=1;i<=num;i++)
{
for(int k=num;k>=i;k--)
{
printf(" ");
}
for(int j=1;j<=i;j++)
{
printf("%d ",j%2);
}
printf("\n");
}
}
Enter the number:
5
1 0 1 0 1
1 0 1 0
1 0 1
1 0
1
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
๐ Explanation
This program combines two major phases to construct a complex binary diamond pattern using nested loops. Unlike focusing on row structures, the inner logic evaluates the expression j%2 to continuously alternate values horizontally.
The first main set of nested loops prints an inverted triangle structure where the rows decrease in size. The second set of nested loops handles the right-side-up triangle layout where rows progressively scale up. Both sections dynamically handle trailing column changes through space formatting.
๐ง Algorithm
- Accept the input tracking value 'num' directly from the console interface.
- Initialize the top half loop cycling backwards down to 1 to format the inverted section.
- Distribute the leading offsets using inner loop restrictions tracking variables 'num' and 'i'.
- Run an independent inner sequence printing j%2 to switch values from 1 to 0 sequentially.
- Duplicate structural properties in the lower phase loop while flipping the outer loop control direction to iterate forward up to 'num'.
⏱ Time Complexity
O(n²)
๐พ Space Complexity
O(1)
๐ Search Description
Master nested loop structures in C programming by building a complex geometric inverted and standard binary diamond pattern utilizing alternating columns.
๐ Keywords
C program binary diamond pattern, alternating binary column pattern, nested loops pyramid code in C, inverted binary pattern printing, C pattern optimization tricks, complex loop structures in C language.
๐ท Hashtags
#CProgramming #PatternPrograms #BinaryDiamond #Coding #Programming #LearnC #NestedLoops #CLanguage #1printf
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment