Problem Statement
Write a program to display the integers in zigzag pattern.
Solution code
#include<stdio.h>
#include <stdlib.h>
int main()
{
int a,b,row,column,printval,diff;
scanf("%d%d",&a,&b);
for(row=1;row<=a;row++)
{
if(row%2==0)
{
printval=(row*a)+(b-1);
diff=-1;
}
else
{
printval=((row-1)*a+1)+(b-1);
diff=1;
}
for(column=1;column<=a;column++)
{
printf("%d ",printval);
printval=printval+diff;
}
printf("\n");
}
return 0;
}
Input
6 12
Output
12 13 14 15 16 17
23 22 21 20 19 18
24 25 26 27 28 29
35 34 33 32 31 30
36 37 38 39 40 41
47 46 45 44 43 42
Program Explanation
- In this zigzag pattern program declare the necessary header files and variables to print the output as per the desired format.
- Using the declared logic in program execute it and get the desired output.
- The declared logic shouldn't like memorize one instead of that it has to come from mind based on problem solving thinking.
- so try to solve more problem you will get your logical thinking power to solve more problems like this in future.