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
C Pattern Program: Diagonal Number Pattern
๐ท C Pattern Program: Diagonal Number Pattern
#include <stdio.h>
int main()
{
int num;
printf("Enter the number:\n");
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
for (int j = 0; j < num; j++)
{
if (j == i)
printf("%d", i + 1);
else if (j == num - 1 - i)
printf("%d", num - i);
else
printf(" ");
}
printf("\n");
}
return 0;
}
๐ Explanation:
This C program prints a square pattern with two diagonal lines:
๐น On the **main diagonal** (from top-left to bottom-right), it prints i+1 (increasing number).
๐น On the **anti-diagonal** (from top-right to bottom-left), it prints num - i (decreasing number).
๐น All other positions are filled with spaces to maintain the structure.
๐น The result is a visually symmetric pattern based on the input size.
๐ Sample Output:
Enter the number:
5
1 5
2 4
3
2 4
1 5
๐ท️ Keywords:
C pattern printing, diagonal number pattern, matrix pattern in C, double diagonal number pattern, C loop-based programs
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