English, asked by hitujondhalekar017, 5 months ago

Write a program to return the difference between the sum of odd numbers and
even numbers from an array of positive integers in java

Note
You are expected to write code in the findOddEven Difference function only
which will receive the first parameter as the number of items in the array and
second parameter as the array itself. You are not required to take input from the
console
Example
Finding the difference between the sum of odd and even numbers from a list of 5
numbers
Input
input1:5
input2: 10 11 7 12 14
Output
-18
Explanation
The first parameter (5) is the size of the array. Next is an array of integers. The
calculation of difference between sum of odd and even numbers is as follows
(11 + 7) - (10 + 12 + 14 = 18-36 = - 18
metti

Answers

Answered by dreamrob
0

Program:

import java.util.*;

public class Main

{

public static void main(String[] args)

{

    Scanner Sc = new Scanner(System.in);

    System.out.print("Enter the number of elements in the array: ");

    int n = Sc.nextInt();

    int A[] = new int[n];

    System.out.print("Enter the elements in the array: ");

    for(int i = 0; i < n; i++)

    {

        A[i] = Sc.nextInt();

    }

    int odd = 0;

    int even = 0;

    for(int i = 0; i < n; i++)

    {

        if(A[i]%2 == 0)

        {

            even = even + A[i];

        }

        else

        {

            odd = odd + A[i];

        }

    }

    int difference = odd - even;

    System.out.print(difference);

}

}

Output:

Enter the number of elements in the array: 5

Enter the elements in the array: 10 11 7 12 14

-18

Similar questions