Skip to content

Instantly share code, notes, and snippets.

@syedjafer
Created March 25, 2018 04:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syedjafer/663538b0d7c4166af5ee1c2a560627ce to your computer and use it in GitHub Desktop.
Save syedjafer/663538b0d7c4166af5ee1c2a560627ce to your computer and use it in GitHub Desktop.
Program to print a snake matrix without using arrays . Input will be given . Eg : 4
#include<stdio.h>
int main()
{
int num;
scanf("%d",&num);
int range = num;
int spaces = num * 2 ; // for spaces
int i,count=0,val=1,val_range=1;
while(range>0)
{
for(i=0;i<spaces;i++)
{
printf(" ");
}
spaces = spaces - 2 ;
if(count == 0)
{
for(i=val_range;i<(num+val_range);i++)
{
printf("%d ",i);
val += 1 ;
}
count = 1 ;
val_range = val-1;
}
// printf("%d\n",val );
else
{
// printf("In else");
// printf("val_range+num %d",val_range+num);
for(i=(val_range+num);i>(val_range);i--)
{
printf("%d ",i);
}
val = val+ num;
val_range = val ;
count = 0;
// break;
}
printf("\n");
range = range - 1 ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment