w3codeworld logo
  • Home
  • C
  • C++
  • Java
  • Python3
  • JavaScript
  • C#
  • PHP
  • Codeigniter
  • Laravel
  • Articles
  1. Home
  2. Articles
  3. Write a java program to convert an array into zig-zag fashion using the function
  • PHP
  • Codeigniter
  • Laravel
  • C
  • C++
  • Java
  • Python3
  • JavaScript
  • C#

Write a java program to convert an array into zig-zag fashion using the function

  • Author: Admin
  • Last Updated: 2020-12-30
  • Category: Java

Write a java program to convert an array into zig-zag fashion using the function. There are you will learn how to print the zig-zag fashion of an array.

 

Take an example to print zig-zag fashion through a java program:

// Write a java program to convert
// an array into zig-zag fashion
import java.util.Scanner;

public class Main {

    public static int i = 1, r = 1;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int[] arru;
        int x = 0;
        // arru - it will store array elements
        // x - size of array

        System.out.print("-----enter the size of the array-----\n");
        x = in .nextInt();

        // mapping size of the array
        arru = new int[x];

        System.out.print("-----Enter the " + x + " elements one by one-----\n");
        for (int i = 0; i < x; i++) {
            arru[i] = in .nextInt();
        }

        // This will change the array position into zig-zag
        System.out.print("\n-----The zig-zag pattern-----\n");
        makeZigZag(arru, x);
        for (int i = 0; i < x; i++)
            System.out.print(arru[i] + ",  ");
        System.out.print("\n");
    }

    // It's the function to generate the zig-zag pattern
    public static void makeZigZag(int arru[], int n) {
        boolean flag = true;
        int temp;

        for (int i = 0; i <= n - 2; i++) {
            if (flag) {
                if (arru[i] > arru[i + 1]) {
                    // It will swap the values
                    temp = arru[i];
                    arru[i] = arru[i + 1];
                    arru[i + 1] = temp;
                }
            } else {
                if (arru[i] < arru[i + 1]) {
                    // It will swap the values
                    temp = arru[i];
                    arru[i] = arru[i + 1];
                    arru[i + 1] = temp;
                }
            }
            flag = !flag;
        }
    }
}

 

Output:

-----enter the size of the array-----
8
-----Enter the 8 elements one by one-----
12
32
45
12
56
32
56
23

-----The zig-zag pattern-----
12,  45,  12,  56,  32,  56,  23,  32,

 

Tags:

# write a program to convert an array into zig-zag fashion python

# write a program of the largest number formed from an array

# write a program to convert an array into zig-zag fashion in java

# zig-zag array code

 

Related Articles

Write a java program to rotate the matrix by k times in a clockwise direction using the function Write a java program to print a hollow square star pattern with diagonal using loops (for and while loop) How to rearrange positive and negative numbers in array in java language Write a java program to convert an array into zig-zag fashion using the function How to print a star pattern without using loop in the java programming language Write a program of spiral pattern printing in java language Write a java program to find the sum of a zig-zag pattern in a given matrix using the recursion How to find the sum of each row and column of a matrix in java language Write a program to find the transpose of a matrix in java language How to print the hollow diamond pattern in java language Write a program to make transformation matrix rotation 90 degrees clockwise in java language Write a java program to find the quotient and remainder How to check whether a number is an integer or float in java language by using the while loop Write a program to draw the half pyramid pattern in java language of stars using the for loop How to print the rhombus pattern in the java language of the stars using the for loop Write a program to print the left & right arrow pattern in java language Write a java program to print the butterfly pattern of the numbers by using the for loop Java program to print diamond pattern of stars by using the for loop Write a java program to draw a rectangle using for loop Write a program to print the pascal triangle pattern in java language Write a program to print Floyd's triangle number pattern in java language Write a program to print the matrix pattern in java language using the for loop Write a java program to find factors of a number using while loop & for loop Write a java program to convert the reverse case of an input character Write a java program to check the input integer number is an Armstrong number using the for loops & while loops How to swap the two integers or real numbers without using the third variable in the java language Java program to check leap year using the if-else and by using the switch case statement Java program to find the roots of a quadratic equation using the sqrt() function with the if-else condition Java program to find GCD of two numbers using the while loop and using the for loop Java program to find LCM of two numbers using for loop and with GCD calculation Java program to calculate the power of N number using for loop and by using pow function Java program to find the Normal & Trace of a square matrix by using the for loop Java program to multiply two same dimension matrices by using the for loop Java program to find the SUM of the N input numbers using arrays with for loop Java program to find the largest and smallest element in an array by using for loop Java program to print the multiplication table of any integer number with multiplication range Java program to print prime numbers from 1 to N using for loop Java program to check input number is even or odd using if-else statement and with conditional operator Java program to find the average of N numbers by using for loop Java program to reverse a string by using the while & for loop Java program to find sum of digits of a number using while loop of any integer number Java program to check palindrome number using while loop or not using function of any number Java program to generate the fibonacci series using iteration Java program to find factorial of a number using for loop Java program to check the input character is a vowel or consonant character Java program of decision making by using the switch case statement Java program of decision making by using if & if-else statement Java program to find the ASCII value of a character Java program to take inputs from the user Java program to enter the P, T, R, and calculate its Compound Interest
Recent Articles
  • Write a program to find all the patterns of 0(1+)0 in the given string
  • C# program to enter two numbers and find their sum
  • Write a c++ program to store information of a student using the structure with the for loop
  • Write a c program to store information of a student using the structure with the for loop
  • How to write a c++ program to find the sum of natural numbers using recursion
  • How to write a c program to find the sum of natural numbers using recursion
  • Write a python program to convert an array into zig-zag fashion using the function
  • C program to perform input/output of all basic data types
  • Write a python program to print a hollow square star pattern with diagonal using loops (for and while loop)
  • Write a c program to make a circular rotation of an array by k positions using the loops
View All Articles
  W3Codeworld

Get the latest updates

  • ABOUT US
  • About Company
  • Contact Us
  • Terms Conditions
  • Privacy Policy
  • Top Tutorials
  • PHP
  • Codeigniter
  • Laravel
  • Top Articles
  • C
  • C++
  • Java
  • Python
  • JavaScript
  • PHP

© Copyright 2020-2021. All Rights Reserved.