To generate Fibonacci series in an inverse spiral form

To generate Fibonacci series in an inverse spiral form

Question:

Write a c program to generate Fibonacci series in an inverse spiral form.

Logic:

  • Get the length of the row from the user.
  • Then, find the index where to start the series.
  • And print the top, left, right and bottom of the series.

Algorithm:

  1.  Get the number of rows from the user.
  2.  Then find the starting position of inverse spiral and it’s also the number of
    rotation in the spiral.
  3. Stores the starting value of Fibonacci into the top row.
  4. And then stores into the right column, then the bottom row.
  5. Then stores into the left column of the spiral.
  6. Repeat step 3 to 5 until the spiral ends.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
   int n,c,i,j,f3,b[30][30];
   int f1=-1,f2=1,a;
   clrscr();
   printf("enter number of rows:");
   scanf("%d",&n);
   c=n;                              //length stored in c to print elements
   a=n/2;                            //finds starting position of the spiral and also finds number of rotation in spiral
   if(n%2==0)                        //checks for even length of rows
   {
	a=a-1;
	n=n-1;
   }
   while(a>=0)
   {
	for(j=a;j<=n-a;j++)          //stores the values in top row
	{
	   f3=f1+f2;
	   f1=f2;
	   f2=f3;
	   b[a][j]=f3;
	}
	for(j=a+1;j<=n-a;j++)        //stores the values in left column
	{
	   f3=f1+f2;
	   f1=f2;
	   f2=f3;
	   b[j][n-a]=f3;
	}
	for(j=n-a-1;j>=a-1;j--)      //stores the values in bottom row
	{
	   f3=f1+f2;
	   f1=f2;
	   f2=f3;
	   b[n-a][j]=f3;
	}
	for(j=n-a-1;j>a-1;j--)       //stores the values in right column
	{
	   f3=f1+f2;
	   f1=f2;
	   f2=f3;
	   b[j][a-1]=f3;
	}a--;                        //decrements for every rotation of spiral
   }
   for(i=0;i<c;i++)
   {
	for(j=0;j<c;j++)
	{
	   printf("%6d",b[i][j]);
	}
	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
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x