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:

#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();
}

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

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.
0 0 votes
Article Rating
Subscribe
Notify of
guest
5.4K Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
5.4K
0
Would love your thoughts, please comment.x
()
x