write a program to return the difference between the sum of odd numbers and even numbers from an array of positive integers
Answers
Answer:
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 170
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
A program to return the difference between the sum of odd numbers and even numbers from an array of positive integers
SOURCE CODE:
#include <stdio.h>
#function calculating the difference between the sum of odd and even numbers.
int findOddEvenDifference(int n, int arr[])
{
int odd = 0, even = 0;
for(int i=0; i<n; i++)
{
if(arr[i]%2==0)
{
even = even + arr[i];
}
else
{
odd = odd + arr[i];
}
}
return odd - even;
}
#main function
int main()
{
int n;
scanf("%d",&n);
int array[n];
#inputting the values in the array
for(int i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
#calling the function
int result = findOddEvenDifference(n, array);
#print the result
printf("%d",result);
return 0;
}
#SPJ3