Question #284397

Create a program that accept six (6) numbers and check whether each number is either odd or even number. Hence, if the sum of odd numbers is greater than the sum of

even numbers, calculate the sum (all odd numbers), otherwise, calculate the sum (all even numbers).


Thanks for answering this I really appreciate it!


Expert's answer

using System;

namespace Negative_6_numbers
{
    internal class Program
    {
        public static void Main(string[] args)
        {
           /*
Create a program that accept six (6) numbers and check whether each number is either odd or
 even number. Hence, if the sum of odd numbers is greater than the sum of
even numbers, calculate the sum (all odd numbers), otherwise,
 calculate the sum (all even numbers).
*/

            int[] array = new int[6];
            int evens = 0;
            int odds = 0;
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write($"Enter a {i + 1} number: ");
                array[i] = int.Parse(Console.ReadLine());
                if (array[i] % 2 == 0) {
                    Console.WriteLine("Even:");
                    evens += array[i];
                }
                else{
                    Console.WriteLine("Odd");
                    odds += array[i];
                }
            }
            if(evens > odds)
                Console.WriteLine($"Sum of even numbers: {evens}");
            else if(odds > evens)
                Console.WriteLine($"Sum of odd numbers: {odds}");
            else
                Console.WriteLine("The sum of even and odd numbers is equal:");
            Console.ReadLine();
        }
    }
}
LATEST TUTORIALS
APPROVED BY CLIENTS