7. Python program to print the elements of an array present on even positionIn 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. Show
In the above array, elements on even position are b and d. ALGORITHM:
PROGRAM:Output: Elements of given array present on even position:
2
4
Python program to print the elements of an array present on even positionPythonServer 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 positionIn 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:
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 ArrayGiven an array of integers. The task is to write a program to find the product of elements at even and odd index positions separately. 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. C++
Java
Python3
C#
PHP
Javascript
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 Python program to print even numbers in a listGiven 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 loopIterate each element in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number. Python3
Output: 10, 4, 66Method 2: Using while loopPython3
Output: 10, 4, 66 Method 3: Using list comprehensionPython3
Output: Even numbers in the list: [10, 4, 66]Method 4: Using lambda expressionsPython3
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 C Program to Print Array Elements present at Even PositionIn 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:
Allow User to Define Array SizeHere 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:
C Online Test « Previous Program Next Program » |