Answer to Question #260540 in C# for Hotdog

Question #260540

Write a C# program that enters 5 elements to an array. Display all the odd numbers and even numbers separately.


1
Expert's answer
2021-11-03T04:22:35-0400
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Input 5 elements in the array :");
                const int n = 5;
                int[] numbers = new int[n];
                for (int i = 0; i < n; i++)
                {
                    Console.Write("element - {0} : ", i+1);
                    string str = Console.ReadLine();
                    numbers[i] = int.Parse(str);
                }
                
                Console.Write("Odd numbers: ");
                for (int i = 0; i < n; i++)
                {
                    if (numbers[i] % 2 == 0)
                        Console.Write("{0} ", numbers[i]);
                }
                Console.WriteLine();

                Console.Write("Even numbers: ");
                for (int i = 0; i < n; i++)
                {
                    if (numbers[i] % 2 == 1)
                        Console.Write("{0} ", numbers[i]);
                }
                Console.WriteLine();
            }
            catch
            {
                Console.WriteLine("Bad number");
                return;
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS