Q:

Program to print the elements of an array present on odd position

belongs to collection: Array Programs

0

Explanation

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.

In the above array, the elements which are on odd positions are a, c and e.

Algorithm

  1. Declare and initialize an array.
  2. Calculate the length of the declared array.
  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.
  4. Print the elements present in odd positions.

Input:

arr = [1, 2, 3, 4, 5]  

Output:

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

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

 

Output:

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

 

C

#include <stdio.h>  
   
int main()  
{  
    //Initialize array   
    int arr[] = {1, 2, 3, 4, 5};   
      
    //Calculate length of array arr  
    int length = sizeof(arr)/sizeof(arr[0]);  
      
    printf("Elements of given array present on odd position: \n");  
    //Loop through the array by incrementing value of i by 2   
    for (int i = 0; i < length; i = i+2) {   
        printf("%d\n", arr[i]);   
    }   
    return 0;  
}

  

 

Output:

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

 

JAVA

public class OddPosition {  
    public static void main(String[] args) {  
          
        //Initialize array   
        int [] arr = new int [] {1, 2, 3, 4, 5};   
          
        System.out.println("Elements of given array present on odd position: ");  
        //Loop through the array by incrementing value of i by 2   
        for (int i = 0; i < arr.length; i = i+2) {   
            System.out.println(arr[i]);   
        }    
    }  
} 

 

 

Output:

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

 

C#

 using System;  
                      
public class OddPosition  
{  
    public static void Main()  
    {  
        //Initialize array   
        int [] arr = new int [] {1, 2, 3, 4, 5};   
          
        Console.WriteLine("Elements of given array present on odd position: ");  
        //Loop through the array by incrementing value of i by 2   
        for (int i = 0; i < arr.Length; i = i+2) {   
            Console.WriteLine(arr[i]);   
        }   
    }  
}  

 

Output:

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

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
//Initialize array   
$arr = array(1, 2, 3, 4, 5);   
   
print("Elements of given array present on odd position: <br>");  
//Loop through the array by incrementing value of i by 2   
for ($i = 0; $i < count($arr); $i = $i+2) {   
    print($arr[$i] . "<br>");   
}    
?>  
</body>  
</html>  

 

Output:

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Program to print the largest element present in an... >>
<< Program to print the elements of an array present ...