Numerical Spiral Pattern solution using c language--programming_info.problem4

 Numerical Spiral Pattern.

This above example is just a reference picture. don't
think it as an input of an array.
you can give your own input.

 1  2  3  4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

#include<stdio.h>
int main()
{
 int n,m,l,k=1;
 scanf("%d",&n);
 m =(l=n);
 int a[m][n] ;
 int row = 0 , col = 0 ;

 while(k <= l*l )
 {

        for(int i = col ; i < n; i ++)
        {
         a[row][i] = k++;
       
  }
  row++;
  for(int i = row; i < m ; i ++)
  {
   a[i][n-1] =k ++;
 
  }
  n --;
  if(row < m)
  {
   for(int i = n - 1; i >= col ; i --)
   { a[m-1][i] = k ++;

   }
  }
  m--;
  if(col < n)
  {
   for(int i = m-1; i >= row ; i --)
   {
   a[i][col] = k ++;
   }
  }
  col ++;

 }
 for(int i = 0 ; i < l ; i ++)
 {
  for(int j = 0 ; j < l ; j ++)
  printf("%d ",a[i][j]);
  printf("\n");
 }
}
Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support