Write a program that accepts a list from the user display the elements stored at odd index position

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

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.

Solution

Python

Output:

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

C

Output:

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

JAVA

Output:

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

C#

Output:

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

PHP

Output:

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

C Program to Print Array Elements present at Odd Position

In this tutorial, we will learn about how to create a program in C that will read array elements and then will print all the array elements present at odd position [index-wise]. The question is, write a program in C to read 10 values in an integer array and print all value stored at odd position. Here is its answer:

#include #include int main[] { int arr[10], i; printf["Enter any 10 array elements: "]; for[i=0; i
Javascript




// Javascript program to find out
// Sum of elements at even and
// odd index positions separately
// Function to calculate sum
function EvenOddSum[arr, n]
{
let even = 0;
let odd = 0;
for [let i = 0; i < n; i++]
{
// Loop to find even, odd sum
if [i % 2 == 0]
even += arr[i];
else
odd += arr[i];
}
document.write["Even index positions sum " + even];
document.write["
" + "Odd index positions sum " + odd];
}
// Driver function
let arr = [ 1, 2, 3, 4, 5, 6 ];
let n = arr.length;
EvenOddSum[arr, n];
// This code is contributed by Mayank Tyagi
Output Even index positions sum 9 Odd index positions sum 12

Method 2:Using slicing in python:

Calculate sum of all even indices using slicing and repeat the same with odd indices and print sum.

Below is the implementation:

Python3




# Python program to find out
# Sum of elements at even and
# odd index positions separately
# Function to calculate Sum
def EvenOddSum[a, n]:
even_sum = sum[a[::2]]
odd_sum = sum[a[1::2]]
print["Even index positions sum", even_sum]
print["Odd index positions sum", odd_sum]
# Driver Function
arr = [1, 2, 3, 4, 5, 6]
n = len[arr]
EvenOddSum[arr, n]
# This code is contributed by vikkycirus
Javascript




// Javascript program to find out
// Sum of elements at even and
// odd index positions separately
// Function to calculate sum
function EvenOddSum[arr, n]
{
let even = 0;
let odd = 0;
for [let i = 0; i < n; i++]
{
// Loop to find even, odd sum
if [i % 2 == 0]
even += arr[i];
else
odd += arr[i];
}
document.write["Even index positions sum " + even];
document.write["
" + "Odd index positions sum " + odd];
}
// Driver function
let arr = [ 1, 2, 3, 4, 5, 6 ];
let n = arr.length;
EvenOddSum[arr, n];
// This code is contributed by avijitmondal1998.
Output Even index positions sum 9 Odd index positions sum 12

This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Article Tags :
Arrays
School Programming
C-Arrays
Traversal
Practice Tags :
Arrays
Traversal
Read Full Article

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
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
Javascript




// 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 + "
"];
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
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 the elements of an array present on odd position

PythonServer Side ProgrammingProgramming

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

Below is a demonstration of the same −

C Program to Find Largest Element in an Array

In this example, you will learn to display the largest element entered by the user in an array.

To understand this example, you should have the knowledge of the following C programming topics:

Video liên quan

Bài mới nhất

Chủ Đề