Python program to print the elements of an array present on odd position

python tutorials and learn python

Created with Sketch.

Python program to print the elements of an array present on odd position

In this program, we need to print the elements of the array which are present in odd positions. This can be accomplished by looping through the array and printing the elements of an array by incrementing i by 2 till the end of the array is reached.

ALGORITHM:

  • STEP 1: Declare and initialize an array.
  • STEP 2: Calculate the length of the declared array.
  • STEP 3: Loop through the array by initializing the value of variable “i” to 0 then incrementing its value by 2, i.e., i=i+2.
  • STEP 4: Print the elements present in odd positions.

PROGRAM:

  1. #Initialize array   
  2. arr = [12345];
  3. print(“Elements of given array present on odd position: “);
  4. #Loop through the array by incrementing the value of i by 2   
  5. for i in range(0, len(arr), 2):
  6.     print(arr[i]);
  7. }

Output:

Elements of given array present on odd position:
1
3
5

Leave a Reply

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