Even indexes#7842

Even indexes (Editorial)

Read the input sequence of numbers and store it in an array. Then, using a loop, print all elements of the array located at even positions.

Algorithm implementation

Declare an array.

int m[101];

Read the number of input elements .

scanf("%d",&n);

Read the input array.

for (i = 0; i < n; i++)
  scanf("%d", &m[i]);

Print the elements of the array located at even positions.

for (i = 0; i < n; i++)
  if (i % 2 == 0) printf("%d ", m[i]);

Java implementation

import java.util.*;

public class Main
{
  public static void main(String[] args)
  {
    Scanner con = new Scanner(System.in);
    int n = con.nextInt();
    int m[] = new int[n];
    for (int i = 0; i < n; i++)
      m[i] = con.nextInt();

    for (int i = 0; i < n; i++)
      if (i % 2 == 0) System.out.print(m[i] + " ");
    con.close();
  }
}

Python implementation

Read the input data.

n = int(input())
lst = list(map(int, input().split()))

Print the elements of the array located at even positions.

for i in range(n):
  if i % 2 == 0: print(lst[i], end = " ")