To print spiral pattern using numbers
This program can print the spiral pattern using numbers.
Logic:
- Get the length of the pattern from the user.
- Then prints the numbers in the pattern using some conditions.
- This gives the pattern in a spiral structure.
Algorithm:
- Get the length of the pattern from the user.
- The pattern is divided into four parts as the top left, top right, left bottom and right bottom.
- For each pattern, the loop can print the largest value.
- After completion of all parts gives spiral structure.
Program:
#include<stdio.h> void main() { int i,j,n; clrscr(); printf("enter the number:"); scanf("%d",&n); for(i=n;i>1;i--) { for(j=n;j>=1;j--)//prints top left of the pattern { if(j>i) printf("%d ",j); else printf("%d ",i); } for(j=2;j<=n;j++)//prints top right of the pattern { if(j>i) printf("%d ",j); else printf("%d ",i); } printf("\n"); } for(i=1; i<=n; i++) { for(j=n;j>=1;j--)//prints left bottom of the pattern { if(j>i) printf("%d ",j); else printf("%d ",i); } for(j=2;j<=n;j++)//prints right bottom of the pattern { if(j>i) printf("%d ",j); else printf("%d ",i); } printf("\n"); } getch(); }