WAP in C to input n numbers in an array, find out the sum of odd nos. and even nos. Display the numbers whose sum is high.

Code Explanation:

To input n numbers in an array, find the sum of odd and even numbers, and display the numbers whose sum is higher, you can follow these steps:

  1. Start by including the necessary header files for input/output operations and defining the maximum size of the array. For example: c #include <stdio.h> #define MAX_SIZE 100

  2. Declare an array of integers with a size of MAX_SIZE to store the input numbers. For example: c int numbers[MAX_SIZE];

  3. Prompt the user to enter the value of n, the number of elements to be entered. For example: c int n; printf("Enter the value of n: "); scanf("%d", &n);

  4. Use a loop to input n numbers from the user and store them in the array. For example: c printf("Enter %d numbers:\n", n); for (int i = 0; i < n; i++) { scanf("%d", &numbers[i]); }

  5. Initialize variables to keep track of the sum of odd and even numbers. For example: c int sumOdd = 0; int sumEven = 0;

  6. Iterate through the array and check if each number is odd or even. If it is odd, add it to the sumOdd variable; if it is even, add it to the sumEven variable. For example: c for (int i = 0; i < n; i++) { if (numbers[i] % 2 == 0) { sumEven += numbers[i]; } else { sumOdd += numbers[i]; } }

  7. Compare the values of sumOdd and sumEven to determine which sum is higher. For example: c if (sumOdd > sumEven) { printf("Sum of odd numbers is higher: %d\n", sumOdd); } else if (sumEven > sumOdd) { printf("Sum of even numbers is higher: %d\n", sumEven); } else { printf("Sum of odd and even numbers is equal: %d\n", sumOdd); }

  8. Display the numbers whose sum is higher. Iterate through the array again and check if each number is odd or even. If the sum of odd numbers is higher, display the odd numbers; if the sum of even numbers is higher, display the even numbers. For example: c if (sumOdd > sumEven) { printf("Odd numbers with higher sum: "); for (int i = 0; i < n; i++) { if (numbers[i] % 2 != 0) { printf("%d ", numbers[i]); } } printf("\n"); } else if (sumEven > sumOdd) { printf("Even numbers with higher sum: "); for (int i = 0; i < n; i++) { if (numbers[i] % 2 == 0) { printf("%d ", numbers[i]); } } printf("\n"); }

  9. End the program. For example: c return 0;

This code allows the user to input n numbers, calculates the sum of odd and even numbers, and displays the numbers whose sum is higher.

Note: The code assumes that the user will enter valid integer inputs and does not include error handling for invalid inputs.

Example:

Here's an example of the complete code:

#include <stdio.h>
#define MAX_SIZE 100

int main() {
    int numbers[MAX_SIZE];
    int n;

    printf("Enter the value of n: ");
    scanf("%d", &n);

    printf("Enter %d numbers:\n", n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &numbers[i]);
    }

    int sumOdd = 0;
    int sumEven = 0;

    for (int i = 0; i < n; i++) {
        if (numbers[i] % 2 == 0) {
            sumEven += numbers[i];
        } else {
            sumOdd += numbers[i];
        }
    }

    if (sumOdd > sumEven) {
        printf("Sum of odd numbers is higher: %d\n", sumOdd);
    } else if (sumEven > sumOdd) {
        printf("Sum of even numbers is higher: %d\n", sumEven);
    } else {
        printf("Sum of odd and even numbers is equal: %d\n", sumOdd);
    }

    if (sumOdd > sumEven) {
        printf("Odd numbers with higher sum: ");
        for (int i = 0; i < n; i++) {
            if (numbers[i] % 2 != 0) {
                printf("%d ", numbers[i]);
            }
        }
        printf("\n");
    } else if (sumEven > sumOdd) {
        printf("Even numbers with higher sum: ");
        for (int i = 0; i < n; i++) {
            if (numbers[i] % 2 == 0) {
                printf("%d ", numbers[i]);
            }
        }
        printf("\n");
    }

    return 0;
}

This code allows the user to input n numbers, calculates the sum of odd and even numbers, and displays the numbers whose sum is higher.