Program to print Sum of even and odd elements in an array
Last Updated :
31 Mar, 2023
Prerequisite - Array Basics
Given an array, write a program to find the sum of values of even and odd index positions separately.
Examples:
Input : arr[] = {1, 2, 3, 4, 5, 6}
Output :Even index positions sum 9
Odd index positions sum 12
Explanation: Here, n = 6 so there will be 3 even
index positions and 3 odd index positions in an array
Even = 1 + 3 + 5 = 9
Odd = 2 + 4 + 6 = 12
Input : arr[] = {10, 20, 30, 40, 50, 60, 70}
Output : Even index positions sum 160
Odd index positions sum 120
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 = 160
Odd = 20 + 40 + 60 = 120
Implementation:
C++
// CPP program to find out
// Sum of elements at even and
// odd index positions separately
#include <iostream>
using namespace std;
// Function to calculate sum
void EvenOddSum(int arr[], int n)
{
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++) {
// Loop to find even, odd sum
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
cout << "Even index positions sum " << even;
cout << "\nOdd index positions sum " << odd;
}
// Driver function
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
EvenOddSum(arr, n);
return 0;
}
Java
// Java program to find out
// Sum of elements at even and
// odd index positions separately
import java.io.*;
class EvenOddSum {
public static void main(String args[])
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int even = 0, odd = 0;
// Loop to find even, odd sum
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
System.out.println("Even index positions sum: " + even);
System.out.println("Odd index positions sum: " + odd);
}
}
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 = 0
odd = 0
for i in range(n):
# Loop to find even, odd Sum
if i % 2 == 0:
even += a[i]
else:
odd += a[i]
print ("Even index positions sum ", even)
print ("nOdd index positions sum ", odd)
# Driver Function
arr = [1, 2, 3, 4, 5, 6]
n = len(arr)
EvenOddSum(arr, n)
# This code is contributed by Sachin Bisht
C#
// C# program to find out
// Sum of elements at even and
// odd index positions separately
using System;
public class GFG {
public static void Main()
{
int[] arr = { 1, 2, 3, 4, 5, 6 };
int even = 0, odd = 0;
// Loop to find even, odd sum
for (int i = 0; i < arr.Length; i++)
{
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
Console.WriteLine("Even index positions"
+ " sum: " + even);
Console.WriteLine("Odd index positions "
+ "sum: " + odd);
}
}
// This code is contributed by Sam007.
PHP
<?php
// PHP program to find out
// Sum of elements at even and
// odd index positions separately
// Function to calculate sum
function EvenOddSum($arr, $n)
{
$even = 0;
$odd = 0;
for ($i = 0; $i < $n; $i++)
{
// Loop to find even, odd sum
if ($i % 2 == 0)
$even += $arr[$i];
else
$odd += $arr[$i];
}
echo("Even index positions sum " . $even);
echo("\nOdd index positions sum " . $odd);
}
// Driver Code
$arr = array( 1, 2, 3, 4, 5, 6 );
$n = sizeof($arr);
EvenOddSum($arr, $n);
// This code is contributed by Ajit.
?>
JavaScript
<script>
// 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("<br>" + "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
</script>
OutputEven index positions sum 9
Odd index positions sum 12
Time Complexity: O(length(arr))
Auxiliary Space: 0(1)
Method 2: Using bitwise & operator
C++
// CPP program to find out
// Sum of elements at even and
// odd index positions separately using bitwise & operator
#include <iostream>
using namespace std;
// Function to calculate sum
void EvenOddSum(int arr[], int n)
{
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++) {
// Loop to find even, odd sum
if (i & 1 != 0)
odd += arr[i];
else
even += arr[i];
}
cout << "Even index positions sum " << even;
cout << "\nOdd index positions sum " << odd;
}
// Driver function
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
EvenOddSum(arr, n);
return 0;
}
//This code is contributed by Vinay Pinjala.
Java
// Java program to find out
// Sum of elements at even and
// odd index positions separately using bitwise & operator
public class Main {
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.length;
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++)
{
// Loop to find even, odd sum
if ((i & 1) != 0) {
odd += arr[i];
}
else {
even += arr[i];
}
}
System.out.println("Even index positions sum: "
+ even);
System.out.println("Odd index positions sum: "
+ odd);
}
}
// This code is contributed by divyansh2212
Python3
# Python program to find out
# Sum of elements at even and
# odd index positions separately using bitwise & operator
# Function to calculate Sum
def EvenOddSum(a, n):
even = 0
odd = 0
for i in range(n):
# Loop to find even, odd Sum
if i &1:
odd += a[i]
else:
even += a[i]
print ("Even index positions sum ", even)
print ("Odd index positions sum ", odd)
# Driver Function
arr = [1, 2, 3, 4, 5, 6]
n = len(arr)
EvenOddSum(arr, n)
# This code is contributed by tvsk
C#
using System;
namespace TestApplication
{
class Program
{
static void Main(string[] args)
{
int[] arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.Length;
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++)
{
// Loop to find even, odd sum
if ((i & 1) != 0)
{
odd += arr[i];
}
else
{
even += arr[i];
}
}
Console.WriteLine("Even index positions sum: " + even);
Console.WriteLine("Odd index positions sum: " + odd);
}
}
}
JavaScript
// JS program to find out
// Sum of elements at even and
// odd index positions separately using bitwise & operator
// 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 & 1 != 0)
odd += arr[i];
else
even += arr[i];
}
console.log("Even index positions sum " + even);
console.log("\nOdd index positions sum " + odd);
}
// Driver function
let arr = [ 1, 2, 3, 4, 5, 6 ];
let n = arr.length;
EvenOddSum(arr, n);
OutputEven index positions sum 9
Odd index positions sum 12
Time Complexity: O(length(arr))
Auxiliary Space: 0(1)
Calculate sum of all even indices using slicing and repeat the same with odd indices and print sum.
Below is the implementation:
C++
#include <iostream>
using namespace std;
// Function to calculate sum
int EvenOddSum(int arr[] , int n)
{
int even = 0;
int odd = 0;
// Loop to find even, odd sum
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
cout<<"Even index positions sum "<<even<<"\n" ;
cout<<"Odd index positions sum " <<odd;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n =sizeof(arr)/sizeof(arr[0]);
EvenOddSum(arr, n);
}
// This code is contributed by ksrikanth0498.
Java
// Java program to find out sum of elements at even
// and odd index positions separately
import java.io.*;
class GFG {
// Function to calculate sum
static void EvenOddSum(int[] arr, int n)
{
int even = 0;
int odd = 0;
// Loop to find even, odd sum
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
System.out.println("Even index positions sum "
+ even);
System.out.print("Odd index positions sum " + odd);
}
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.length;
EvenOddSum(arr, n);
}
}
// This code is contributed by lokeshmvs21.
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
C#
// C# program to find out sum of elements at even
// and odd index positions separately
using System;
public class GFG {
// Function to calculate sum
static void EvenOddSum(int[] arr, int n)
{
int even = 0;
int odd = 0;
// Loop to find even, odd sum
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
Console.WriteLine("Even index positions sum "
+ even);
Console.WriteLine("Odd index positions sum " + odd);
}
static public void Main()
{
// Code
int[] arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.Length;
EvenOddSum(arr, n);
}
}
// This code is contributed by lokeshmvs21.
JavaScript
<script>
// 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("<br>" + "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.
</script>
OutputEven index positions sum 9
Odd index positions sum 12
Time Complexity: O(n) where n is no of elements in given array
Auxiliary Space: 0(1)
Method 2: Using bitwise | operator
C++14
// C++ program to find out
// Sum of elements at even and
// odd index positions separately using bitwise | operator
#include<bits/stdc++.h>
using namespace std;
// Function to calculate Sum
void EvenOddSum(vector<int>a, int n)
{
int even = 0;
int odd = 0;
for(int i=0; i<n; i++)
{
// Loop to find even, odd Sum
if ((i|1)==i)
odd+=a[i];
else
even+=a[i];
}
cout<< "Even index positions sum "<< even<<endl;
cout<< "Odd index positions sum "<< odd<<endl;
}
// Driver Function
int main()
{
vector<int>arr = {1, 2, 3, 4, 5, 6};
int n = arr.size();
EvenOddSum(arr, n);
}
Python3
# Python program to find out
# Sum of elements at even and
# odd index positions separately using bitwise | operator
# Function to calculate Sum
def EvenOddSum(a, n):
even = 0
odd = 0
for i in range(n):
# Loop to find even, odd Sum
if i|1==i:
odd+=a[i]
else:
even+=a[i]
print ("Even index positions sum ", even)
print ("Odd index positions sum ", odd)
# Driver Function
arr = [1, 2, 3, 4, 5, 6]
n = len(arr)
EvenOddSum(arr, n)
JavaScript
// JavaScript program to find out
// Sum of elements at even and
// odd index positions separately using bitwise | operator
// Function to calculate Sum
function EvenOddSum(a, n) {
let even = 0;
let odd = 0;
for(let i = 0; i < n; i++) {
// Loop to find even, odd Sum
if ((i|1) === i)
odd += a[i];
else
even += a[i];
}
console.log("Even index positions sum ", even);
console.log("Odd index positions sum ", odd);
}
// Driver Function
const arr = [1, 2, 3, 4, 5, 6];
const n = arr.length;
EvenOddSum(arr, n);
Java
import java.util.*;
class Main {
// Function to calculate Sum of elements at even and odd
// index positions separately
static void EvenOddSum(List<Integer> a, int n)
{
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++) {
// Loop to find even, odd Sum
if ((i | 1) == i) {
odd += a.get(i);
}
else {
even += a.get(i);
}
}
// Print the sum of elements at even and odd index
// positions
System.out.println("Even index positions sum "
+ even);
System.out.println("Odd index positions sum "
+ odd);
}
// Driver Function
public static void main(String[] args)
{
// Create a list of integers
List<Integer> arr = new ArrayList<>(
Arrays.asList(1, 2, 3, 4, 5, 6));
// Get the size of the list
int n = arr.size();
// Call the EvenOddSum function
EvenOddSum(arr, n);
}
}
C#
// C# program to find out
// Sum of elements at even and
// odd index positions separately using bitwise | operator
using System;
using System.Collections.Generic;
class MainClass {
// Function to calculate Sum
static void EvenOddSum(List<int> a, int n)
{
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++) {
// Loop to find even, odd Sum
if ((i | 1) == i)
odd += a[i];
else
even += a[i];
}
Console.WriteLine("Even index positions sum "
+ even);
Console.WriteLine("Odd index positions sum " + odd);
}
// Driver Function
public static void Main(string[] args)
{
List<int> arr = new List<int>{ 1, 2, 3, 4, 5, 6 };
int n = arr.Count;
EvenOddSum(arr, n);
}
}
OutputEven index positions sum 9
Odd index positions sum 12
Time Complexity: O(length(arr))
Auxiliary Space: 0(1)
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read