Pattern – ZigZag Number Square

                 Pattern - ZigZag Number Square

 

Given an integer N as the input, print the pattern as given in the Example Input/Output section.
Input Format:
The first line contains N.
Output Format:
N lines containing the desired pattern.
Boundary Conditions:
2 <= N <= 50
Example Input/Output 1:
Input:
5
Output:
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
Example Input/Output 2:
Input:
8
Output:
1 2 3 4 5 6 7 8
16 15 14 13 12 11 10 9
17 18 19 20 21 22 23 24
32 31 30 29 28 27 26 25
33 34 35 36 37 38 39 40
48 47 46 45 44 43 42 41
49 50 51 52 53 54 55 56
64 63 62 61 60 59 58 57

Code:

Java:

import java.util.Scanner;
public class Hello {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        printPattern(N);
    }
static void printPattern(int( N) {
int i,j,x=1,flag=0;
for(i=1;i<=N;i++)
{
    if(i!=1&&i%2==0)
           x+=N-1;
    else if(i%2==1&&i!=1)
        x+=N+1;
    for(j=1;j<=N;j++)
    {
        if(i%2==1)
           System.out.print(x++ +" ");
        else
            System.out.print(x--+" ");
    }
    System.out.println();
}
}

C language:

#include<stdio.h>
void printPattern(int N)
{
int i,j,x=1;
for(i=1;i<=N;i++)
{
    if(i!=1&&i%2==0)
        x+=N-1;
    else if(i!=1&&i%2==1)
        x+=N+1;
    for(j=1;j<=N;j++)
    {
        if(i%2==1)
            printf("%d ",x++);
        else
            printf("%d ",x--);
    }
    printf("n");
}
}
int main()(
{
    int N;
    scanf("%d",&N);
    printPattern(N);
    return 0;
}

Please do comment If u have any Queries!

Add a Comment

Your email address will not be published. Required fields are marked *