Created
March 25, 2018 04:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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