Use a Loop to Display elements from a given list that are present at even index positions

7. Python program to print the elements of an array present on even position

In this program, we need to print the element which is present on even position. Even positioned element can be found by traversing the array and incrementing the value of i by 2.

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

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

PROGRAM:

Output:

Elements of given array present on even position: 2 4

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

PythonServer Side ProgrammingProgramming

When it is required to print the elements of a list that are present at even index/position, a loop can be used to iterate over the elements, and only check the even positions in the list by specifying the step size as 2 in the range function.

Below is a demonstration of the same −

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

« Previous Next »

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

In this example, we will see a Python program through which we can print the elements of an array present on even positions.

Even position element can be found by traversing the given array and incrementing the value of i by 2. In this way the i will initialize with 1 and after incrementing it will be 3, 5, 7, 9, etc. because the array indexing starts with 0.

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 1 then incrementing its value by 2.
  • STEP 4:Print the elements present in even positions.
Program: #Initialize array arr = [9,10,11,12,13,14,15]; 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:
10
12
14
« Previous Next »

Program to print product of even and odd indexed elements in an Array

Given an array of integers. The task is to write a program to find the product of elements at even and odd index positions separately.
Note: 0-based indexing is considered for the array. That is the index of the first element in the array is zero.
Examples:

Input : arr = {1, 2, 3, 4, 5, 6} Output : Even Index Product : 15 Odd Index Product : 48 Explanation: Here, N = 6 so there will be 3 even index positions and 3 odd index positions in the array Even = 1 * 3 * 5 = 15 Odd = 2 * 4 * 6 = 48 Input : arr = {10, 20, 30, 40, 50, 60, 70} Output : Even Index Product : 105000 Odd Index Product : 48000 Explanation: Here, n = 7 so there will be 3 odd index positions and 4 even index positions in an array Even = 10 * 30 * 50 * 70 = 1050000 Odd = 20 * 40 * 60 = 48000

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Traverse the array and keep two variables even and odd to store the product of elements and even and odd indexes respectively. While traversing check if the current index is even or odd, i.e. (i%2) is zero or not. If even multiply current element with even indexed product otherwise multiply it with odd indexed product.
Below is the implementation of the above approach:

C++




// CPP program to find product of elements
// at even and odd index positions separately
#include <iostream>
using namespace std;
// Function to calculate product
void EvenOddProduct(int arr[], int n)
{
int even = 1;
int odd = 1;
for (int i = 0; i < n; i++) {
// Loop to find even, odd product
if (i % 2 == 0)
even *= arr[i];
else
odd *= arr[i];
}
cout << "Even Index Product : " << even << endl;
cout << "Odd Index Product : " << odd;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
EvenOddProduct(arr, n);
return 0;
}
Java




// Java program to find product of elements
// at even and odd index positions separately
class GFG
{
// Function to calculate product
static void EvenOddProduct(int arr[], int n)
{
int even = 1;
int odd = 1;
for (int i = 0; i < n; i++) {
// Loop to find even, odd product
if (i % 2 == 0)
even *= arr[i];
else
odd *= arr[i];
}
System.out.println("Even Index Product : " + even);
System.out.println("Odd Index Product : " + odd);
}
// Driver Code
public static void main(String []args)
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = arr.length;
EvenOddProduct(arr, n);
}
// This code is contributed by ihritik
}
Python3




# Python3 program to find product of elements
# at even and odd index positions separately
# Function to calculate product
def EvenOddProduct(arr, n):
even = 1
odd = 1
for i in range (0,n):
# Loop to find even, odd product
if (i % 2 == 0):
even *= arr[i]
else:
odd *= arr[i]
print("Even Index Product : " , even)
print("Odd Index Product : " , odd)
# Driver Code
arr = 1, 2, 3, 4, 5, 6
n = len(arr)
EvenOddProduct(arr, n)
# This code is contributed by ihritik
C#




// C# program to find product of elements
// at even and odd index positions separately
using System;
class GFG
{
// Function to calculate product
static void EvenOddProduct(int []arr, int n)
{
int even = 1;
int odd = 1;
for (int i = 0; i < n; i++) {
// Loop to find even, odd product
if (i % 2 == 0)
even *= arr[i];
else
odd *= arr[i];
}
Console.WriteLine("Even Index Product : " + even);
Console.WriteLine("Odd Index Product : " + odd);
}
// Driver Code
public static void Main()
{
int []arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.Length;
EvenOddProduct(arr, n);
}
// This code is contributed by ihritik
}
PHP




<?php
// PHP program to find product of elements
// at even and odd index positions separately
// Function to calculate product
function EvenOddProduct($arr, $n)
{
$even = 1;
$odd = 1 ;
for($i=0;$i<$n;$i++)
{
// Loop to find even, odd product
if ($i % 2 == 0)
$even *= $arr[$i];
else
$odd *= $arr[$i];
}
echo "Even Index Product: " .$even;
echo "\n";
echo "Odd Index Product: " .$odd;
}
// Driver Code
$arr = array(1, 2, 3, 4, 5, 6) ;
$n = sizeof($arr);
EvenOddProduct($arr, $n);
// This code is contributed by ihritik
?>
Javascript




<script>
// Javascript program to find product of elements
// at even and odd index positions separately
// Function to calculate product
function EvenOddProduct(arr, n)
{
let even = 1;
let odd = 1;
for (let i = 0; i < n; i++) {
// Loop to find even, odd product
if (i % 2 == 0)
even *= arr[i];
else
odd *= arr[i];
}
document.write("Even Index Product : " + even + "<br>");
document.write("Odd Index Product : " + odd);
}
// Driver Code
let arr = [ 1, 2, 3, 4, 5, 6 ];
let n = arr.length;
EvenOddProduct(arr, n);
// This code is contributed by Mayank Tyagi
</script>
Output: Even Index Product : 15 Odd Index Product : 48

Time complexity : O(n)




Article Tags :
Arrays
School Programming
Technical Scripter
Technical Scripter 2018
Practice Tags :
Arrays
Read Full Article

Python program to print even numbers in a list

Given a list of numbers, write a Python program to print all even numbers in the given list.

Example:

Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] Input: list2 = [12, 14, 95, 3] Output: [12, 14]

Method 1: Using for loop

Iterate each element in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number.

Python3




# Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
print(num, end=" ")

Output:

10, 4, 66

Method 2: Using while loop

Python3




# Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 24, 4, 45, 66, 93]
num = 0
# using while loop
while(num < len(list1)):
# checking condition
if list1[num] % 2 == 0:
print(list1[num], end=" ")
# increment num
num += 1

Output:



10, 4, 66

Method 3: Using list comprehension

Python3




# Python program to print even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# using list comprehension
even_nos = [num for num in list1 if num % 2 == 0]
print("Even numbers in the list: ", even_nos)

Output:

Even numbers in the list: [10, 4, 66]

Method 4: Using lambda expressions

Python3




# Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
# we can also print even no's using lambda exp.
even_nos = list(filter(lambda x: (x % 2 == 0), list1))
print("Even numbers in the list: ", even_nos)

Output:

Even numbers in the list: [10, 4, 66]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
School Programming
Python list-programs
python-list
Practice Tags :
python-list
Read Full Article

C Program to Print Array Elements present at Even Position

In this tutorial, we will learn about how to create a program in C that will ask from user to enter array elements to print all the array elements present at even index number or even positions. Here is the program.

#include<stdio.h> #include<conio.h> int main() { int arr[10], i; printf("Enter any 10 array elements: "); for(i=0; i<10; i++) scanf("%d", &arr[i]); printf("\nValues stored at even position are:\n"); for(i=0; i<10; i++) { if(i%2==0) printf("%d ", arr[i]); } getch(); return 0; }

As the program was written under Code::Blocks IDE, therefore after successful build and run, here is the first snapshot of the sample run:

As we all knows that the indexing in array starts form 0, therefore here is the second snapshot of the sample run. User has to provide any 10 numbers as 10 array elements for the given array and press ENTER key to see the following output:

Here are the list of some main steps used in above program:

  • Receive any 10 numbers as array elements
  • Create a for loop stars from 0th number (array element) to 9th number (array element)
  • Inside the for loop, check whether the index number is an even number or not
  • If it is an even number, then print the value present at that even index number
  • In this way, we will see all the array elements present at even index number on output screen after running the program

Allow User to Define Array Size

Here is the modified version of the above program. In this program, we have allowed the user to enter the size of the array at run-time:

#include<stdio.h> #include<conio.h> int main() { int arr[100], i, limit; printf("How many elements you want to store inside the array: "); scanf("%d", &limit); printf("Enter any %d array elements: ", limit); for(i=0; i<limit; i++) scanf("%d", &arr[i]); printf("\n\nValues stored at even position (with array and its index) are:\n"); for(i=0; i<limit; i++) { if(i%2==0) printf("arr[%d] = %d\n", i, arr[i]); } getch(); return 0; }

Here is the final snapshot of sample run:

Here are some of the main steps used in above program:

  • Receive the size for the array say 20
  • Now receive any 20 numbers as 20 array elements
  • And then follow all the steps as told in previous program
  • That is to find the even index number one by one to print all the values stored at even index numbers

C Online Test

« Previous Program Next Program »

Video liên quan

Postingan terbaru

LIHAT SEMUA