Print Numbers in Snake Pattern in C / C++ / Java / Python / C#

Snake pattern

Programs to print the numbers in snake pattern have been given here.






1. Program & output to print numbers in snake pattern




1.1. C Program & output to print numbers in snake pattern

Code has been copied
/************************
  alphabetacoder.com
C program to print the  
numbers in snake pattern
*************************/

#include <stdio.h>

int main() {
    // declare variables
    int row, col, num, i, j, k;

    // take input
    printf("Enter the number of rows: ");
    scanf("%d", & row);
    printf("Enter the number of columns: ");
    scanf("%d", & col);

    // new line
    printf("\n");

    // initilize
    num = 1;
    k = 1;

    // display the pattern
    for (i = 1; i <= row; i++) {
        for (j = 1; j <= col; j++) {
            // display value
            printf("%d\t", num);
            // increment
            num = num + k;
        }
        // when the index of the 
        // next row is even 
        if (i % 2 == 1) {
            // calculate the value with 
            // which the next row starts
            num = num + col - 1;
            // increment value
            k = -1;
        }
        // when the index of the 
        // next row is odd 
        else {
            // calculate the value with 
            // which the next row starts
            num = num + col + 1;
            // increment value
            k = 1;
        }
        // new line
        printf("\n");
    }

    return 0;
}

Output


Enter the number of rows: 4

Enter the number of columns: 2


1 2 

4 3 

5 6 

8 7 




1.2. C++ Program & output to print numbers in snake pattern

Code has been copied
/************************
  alphabetacoder.com
C++ program to print the  
numbers in snake pattern
*************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, col, num, i, j, k;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;
    cout << "Enter the number of columns: ";
    cin >> col;

    // new line
    cout << endl;

    // initilize
    num = 1;
    k = 1;

    // display the pattern
    for (i = 1; i <= row; i++) {
        for (j = 1; j <= col; j++) {
            // display value
            cout << num << "\t";
            // increment
            num = num + k;
        }
        // when the index of the 
        // next row is even 
        if (i % 2 == 1) {
            // calculate the value with 
            // which the next row starts
            num = num + col - 1;
            // increment value
            k = -1;
        }
        // when the index of the 
        // next row is odd 
        else {
            // calculate the value with 
            // which the next row starts
            num = num + col + 1;
            // increment value
            k = 1;
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 7

Enter the number of columns: 3


1  2  3 

6  5  4 

7  8  9 

12 11 10 

13 14 15 

18 17 16 

19 20 21 




1.3. Java Program & output to print numbers in snake pattern

Code has been copied
/************************
  alphabetacoder.com
Java program to print the  
numbers in snake pattern
*************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);
        // declare variables
        int row, col, num, i, j, k;

        // take input 
        System.out.print("Enter the number of rows: ");
        row = sc.nextInt();
        System.out.print("Enter the number of columns: ");
        col = sc.nextInt();

        // new line
        System.out.println("");

        // initilize
        num = 1;
        k = 1;

        // display the pattern
        for (i = 1; i <= row; i++) {
            for (j = 1; j <= col; j++) {
                // display value
                System.out.print(num + "\t");
                // increment
                num = num + k;
            }
            // when the index of the 
            // next row is even 
            if (i % 2 == 1) {
                // calculate the value with 
                // which the next row starts
                num = num + col - 1;
                // increment value
                k = -1;
            }
            // when the index of the 
            // next row is odd 
            else {
                // calculate the value with 
                // which the next row starts
                num = num + col + 1;
                // increment value
                k = 1;
            }
            // new line
            System.out.println("");
        }
    }
}

Output


Enter the number of rows: 6

Enter the number of columns: 2


1  2 

4  3 

5  6 

8  7 

9  10 

12 11 




1.4. Python Program & output to print numbers in snake pattern

Code has been copied
#***************************
#  alphabetacoder.com
#Python program to print the  
#numbers in snake pattern
#****************************


# take input 
row = int(input("Enter the number of rows: "))
col = int(input("Enter the number of columns: "))

# new line
print("")

# initilize
num = 1
k = 1

# display the pattern
for i in range(1, row + 1):
    for j in range(1, col + 1):
        # display value
        print(num,"\t", end = "")
        # increment
        num = num + k
        
    # when the index of the 
    # next row is even 
    if i % 2 == 1:
        # calculate the value with 
        # which the next row starts
        num = num + col - 1
        # increment value
        k = -1

    # when the index of the 
    # next row is odd 
    else:
        # calculate the value with 
        # which the next row starts
        num = num + col + 1
        # increment value
        k = 1
 
    # new line
    print("")

Output


Enter the number of rows: 9

Enter the number of columns: 2


1  2 

4  3 

5  6 

8  7 

9  10 

12 11 

13 14 

16 15 

17 18 




1.5. C# Program & output to print numbers in snake pattern

Code has been copied
/************************
  alphabetacoder.com
C# program to print the  
numbers in snake pattern
*************************/

using System;

namespace Snake {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int row, col, num, i, j, k;

            // take input
            Console.Write("Enter the number of rows: ");
            row = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the number of columns: ");
            col = Convert.ToInt32(Console.ReadLine());

            // new line
            Console.WriteLine("");

            // initilize
            num = 1;
            k = 1;

            // display the pattern
            for (i = 1; i <= row; i++) {
                for (j = 1; j <= col; j++) {
                    // display value
                    Console.Write(num + "\t");
                    // increment
                    num = num + k;
                }
                // when the index of the 
                // next row is even 
                if (i % 2 == 1) {
                    // calculate the value with 
                    // which the next row starts
                    num = num + col - 1;
                    // increment value
                    k = -1;
                }
                // when the index of the 
                // next row is odd 
                else {
                    // calculate the value with 
                    // which the next row starts
                    num = num + col + 1;
                    // increment value
                    k = 1;
                }
                // new line
                Console.WriteLine("");
            }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the number of rows: 8

Enter the number of columns: 4


1  2  3  4 

8  7  6  5 

9  10 11 12 

16 15 14 13 

17 18 19 20 

24 23 22 21 

25 26 27 28 

32 31 30 29