This C program accepts two numbers R and C as input and prints the vertical zigzag C columns.
Example 1
Input
5 5
Output
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
Example 2
Input
2 3
Output
1 4 5
2 3 6
Program
#include <stdio.h>
int main()
{
int a[10][10],R,C,i,j,k=1;
scanf("%d%d",&R,&C);
for(i=1;i<=C;i++)
{
for(j=1;j<=R;j++)
{
if (i%2==1)
a[i][j]=k++;
else
a[i][j]=--k;
}
k=k+R;
}
for(i=1;i<=R;i++)
{
for(j=1;j<=C;j++)
printf("%d ",a[j][i]);
printf("\n");
}
}
OUTPUT
5 5
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
Example 1
Input
5 5
Output
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
Example 2
Input
2 3
Output
1 4 5
2 3 6
Program
#include <stdio.h>
int main()
{
int a[10][10],R,C,i,j,k=1;
scanf("%d%d",&R,&C);
for(i=1;i<=C;i++)
{
for(j=1;j<=R;j++)
{
if (i%2==1)
a[i][j]=k++;
else
a[i][j]=--k;
}
k=k+R;
}
for(i=1;i<=R;i++)
{
for(j=1;j<=C;j++)
printf("%d ",a[j][i]);
printf("\n");
}
}
OUTPUT
5 5
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
No comments:
Post a Comment