To print spiral pattern using numbers

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:

  1. Get the length of the pattern from the user.
  2. The pattern is divided into four parts as the top left, top right, left bottom and right bottom.
  3. For each pattern, the loop can print the largest value.
  4. After completion of all parts gives spiral structure.

Program:

[code lang=”c”]
#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();
}
[/code]

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.

Leave a Reply

Your email address will not be published. Required fields are marked *