TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

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

Program to print the elements of an array present on even position on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Q. Program to print the elements of an array present on even position.

Explanation

In this program, we need to print the element which is present in even position.

Even positioned element can be found by traversing the array and incrementing the value of i by 2.

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

In the above array, elements on even position are b and d.

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 1 (because first even positioned element lies on i = 1) then incrementing its value by 2, i.e., i=i+2.
  4. Print the elements present in even positions.

Solution

Python

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

#Here, i will start from 1 as first even positioned element is present at position 1.
for i in range(1, len(arr), 2):
    print(arr[i]); 

Output:

Elements of given array present on even position: 
2
4

C

#include 
 
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 even position: \n");
    //Loop through the array by incrementing value of i by 2
    //Here, i will start from 1 as first even positioned element is present at position 1.
    for (int i = 1; i < length; i = i+2) { 
        printf("%d\n", arr[i]); 
    } 
    return 0;
}

Output:

Elements of given array present on even position: 
2
4

JAVA

public class EvenPosition {
    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 even position: ");
        //Loop through the array by incrementing value of i by 2
        //Here, i will start from 1 as first even positioned element is present at position 1.
        for (int i = 1; i < arr.length; i = i+2) { 
            System.out.println(arr[i]); 
        }  
    }
}

Output:

Elements of given array present on even position: 
2
4

C#

using System;
public class EvenPosition
{
    public static void Main()
    {
        //Initialize array 
        int [] arr = new int [] {1, 2, 3, 4, 5}; 
        
        Console.WriteLine("Elements of given array present on even position: ");
        //Loop through the array by incrementing value of i by 2
        //Here, i will start from 1 as first even positioned element is present at position 1.
        for (int i = 1; i < arr.Length; i = i+2) { 
            Console.WriteLine(arr[i]); 
        }  
    }
}

Output:

Elements of given array present on even position: 
2
4

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//Initialize array 
$arr = array(1, 2, 3, 4, 5); 
 
print("Elements of given array present on even position: <br>");
//Loop through the array by incrementing value of i by 2
//Here, i will start from 1 as first even positioned element is present at position 1.
for ($i = 1; $i < count($arr); $i = $i+2) { 
    print($arr[$i] . "<br>"); 
}  
?>
</body>
</html> 

Output:

Elements of given array present on even position: 
2
4

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf