An array of n integers
is given. Print all elements located at even positions. The indexing of
elements starts from 0.
Input. The first line
contains the integer n. The second line contains n integers.
Each integer does not exceed 100 in absolute value.
Output. Print the elements
of the array with even indices.
Sample input |
Sample output |
7 14 17 16 3 7 19 9 |
14 3 17 9 |
loops
Algorithm
analysis
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 n.
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 = " ")