Java Program to print an alternate elements from an array

Alternate elements

This program illustrate the concept to print a alternate elements from an array.Array stores the element in contiguous memory location.Users can input only one data type of elements in the array at one time.

  • We have first defined the size of array i.e. “int x=new int[100]”.
  • After getting the size of array users will enter the elements into the array
  • Once user will enter all the elements.Program will give output using logic “i=i+2”.
  • import java.util.*;
    class scan
    {
    	 public static void main(String arg[])
    	 {
     int x[]=new int[100];
     int i,y;
     Scanner kb=new Scanner(System.in);
     System.out.println("Enter size of array:- ");
    y=kb.nextInt();
     System.out.println("Enter elements in array:- ");
     for(i=0;i<y;i++)
      {
      x[i]=kb.nextInt();
      }
    System.out.println("Alternate elements in array :- ");
    for(i=0;i<y;i=i+2)
     {
      System.out.println(x[i]);
     }
    }
    }
    

    OUTPUT:-

    alternate elements


    Comment below if you are facing any problem.


    Java program to insert an element into sorted array
    Java program to sort entered element in the Array
    Java program to search an entered element in array


    Summary
    Java Program to print an alternate elements from an array
    Article Name
    Java Program to print an alternate elements from an array
    Description
    This program illustrate the concept to print a alternate elements from an array.Array stores the element in contiguous memory location.
    Author
    Publisher Name
    Scanfcode
    Publisher Logo

Leave a Reply

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