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:
- Get the number of rows from the user.
- Then find the starting position of inverse spiral and it’s also the number of
rotation in the spiral. - Stores the starting value of Fibonacci into the top row.
- And then stores into the right column, then the bottom row.
- Then stores into the left column of the spiral.
- 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(); }