Write a Python Program to Find Sum of Even and Odd Numbers in an Array using the for loop range. The if condition (if (evenOddSumArr[i] % 2 == 0)) checks the array item is divisible by two. If True, we add that array item to evnArrSum. Else add that array value to oddArrSum.
# Sum of Even and Odd in Array import numpy as np evenOddSumArr = np.array([10, 25, 199, 450, 70, 111, 190, 7]) evenArrSum = 0 oddArrSum = 0 for i in range(len(evenOddSumArr)): if (evenOddSumArr[i] % 2 == 0): evenArrSum = evenArrSum + evenOddSumArr[i] else: oddArrSum = oddArrSum + evenOddSumArr[i] print("The Sum of Even Numbers in evenOddSumArr Array = ", evenArrSum) print("The Sum of Odd Numbers in evenOddSumArr Array = ", oddArrSum)
Python Program to Find Sum of Even and Odd Numbers in an Array
In this Python example, we used the for loop to iterate the array items. The numpy mod and remainder functions check the remainder of each array item by two is equal to zero.
# Sum of Even and Odd in Array import numpy as np evenOddSumArr = np.array([10, 199, 14, 450, 170, 191, 200, 700]) evenArrSum = evenArrSum1 = evenArrSum2 = 0 oddArrSum = oddArrSum1 = oddArrSum2 = 0 for i in evenOddSumArr: if (i % 2 == 0): evenArrSum = evenArrSum + i else: oddArrSum = oddArrSum + i print("The Sum of Even Numbers in evenOddSumArr Array = ", evenArrSum) print("The Sum of Odd Numbers in evenOddSumArr Array = ", oddArrSum) print("\n=== Using mod function===") for i in evenOddSumArr: if (np.mod(i, 2) == 0): evenArrSum1 = evenArrSum1 + i else: oddArrSum1 = oddArrSum1 + i print("The Sum of Even Numbers in evenOddSumArr Array = ", evenArrSum1) print("The Sum of Odd Numbers in evenOddSumArr Array = ", oddArrSum1) print("\n=== Using remainder function===") for i in evenOddSumArr: if (np.remainder(i, 2) == 0): evenArrSum2 = evenArrSum2 + i else: oddArrSum2 = oddArrSum2 + i print("The Sum of Even Numbers in evenOddSumArr Array = ", evenArrSum2) print("The Sum of Odd Numbers in evenOddSumArr Array = ", oddArrSum2)
The Sum of Even and Odd Numbers in a Python Numpy Array output
The Sum of Even Numbers in evenOddSumArr Array = 1544
The Sum of Odd Numbers in evenOddSumArr Array = 390
=== Using mod function===
The Sum of Even Numbers in evenOddSumArr Array = 1544
The Sum of Odd Numbers in evenOddSumArr Array = 390
=== Using remainder function===
The Sum of Even Numbers in evenOddSumArr Array = 1544
The Sum of Odd Numbers in evenOddSumArr Array = 390
Python Program to Calculate the Sum of Even and Odd Numbers in an Array using the While loop.
# Sum of Even and Odd in Array import numpy as np evenOddSumArr = np.array([10, 25, 40, 75, 100, 125, 1100, 175]) i = 0 evenArrSum = evenArrSum1 = evenArrSum2 = 0 oddArrSum = oddArrSum1 = oddArrSum2 = 0 while (i < len(evenOddSumArr)): if (np.equal(evenOddSumArr[i] % 2, 0)): evenArrSum = evenArrSum + evenOddSumArr[i] else: oddArrSum = oddArrSum + evenOddSumArr[i] i = i + 1 print("The Sum of Even Numbers in evenOddSumArr Array = ", evenArrSum) print("The Sum of Odd Numbers in evenOddSumArr Array = ", oddArrSum)
The Sum of Python Numpy Array Even and Odd Numbers using a while loop output
The Sum of Even Numbers in evenOddSumArr Array = 1250
The Sum of Odd Numbers in evenOddSumArr Array = 400
In this Python numpy array example, we created a (SumEvenOddNumbers(evenOddSumArr)) function that returns the sum of Even numbers and odd numbers.
# Sum of Even and Odd in Array import numpy as np def SumEvenOddNumbers(evenOddSumArr): evenArrSum = 0 oddArrSum = 0 for i in evenOddSumArr: if (np.remainder(i, 2) == 0): evenArrSum = evenArrSum + i else: oddArrSum = oddArrSum + i return evenArrSum, oddArrSum evenOddSumArr = np.array([10, 20, 25, 55, 100, 85, 200]) evensum, oddsum = SumEvenOddNumbers(evenOddSumArr) print("The Sum of Even Numbers in evenOddSumArr Array = ", evensum) print("The Sum of Odd Numbers in evenOddSumArr Array = ", oddsum)
Python Numpy Array even and odd sum using functions output
The Sum of Even Numbers in evenOddSumArr Array = 330
The Sum of Odd Numbers in evenOddSumArr Array = 165