Answer to Question #274265 in C# for leslie

Question #274265

Write a sample program that enters 10 numbers and Display all on the 1 st stack. Then after, display all the even numbers on the 2 nd stack and odd numbers on the 3 rd stack.


1
Expert's answer
2021-12-01T18:36:11-0500
using System;
using System.Collections.Generic;

namespace ConsoleAppEvenAndOddNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the 10 numbers separated by commas.");
            string enteredString = Console.ReadLine();
            try
            {
                string[] arrayString = enteredString.Split(',');
                if (arrayString.Length != 10)
                {
                    throw new ArgumentException("The count of entered numbers must be 10.");
                }
                Stack<int> allNumbers = new Stack<int>();
                Stack<int> evenNumbers = new Stack<int>();
                Stack<int> oddNumbers = new Stack<int>();
                for (int i = 0; i < arrayString.Length; i++)
                {
                    int tempNumber;
                    if (!int.TryParse(arrayString[i], out tempNumber))
                    {
                        throw new ArgumentException($"The entered element: {arrayString[i]} cannot be converted to a number.");
                    }
                    allNumbers.Push(tempNumber);
                    if (tempNumber % 2 == 0)
                    {
                        evenNumbers.Push(tempNumber);
                    }
                    else
                    {
                        oddNumbers.Push(tempNumber);
                    }
                }
                Console.WriteLine("Display all numbers:");
                foreach (int number in allNumbers)
                {
                    Console.WriteLine(number);
                }
                Console.WriteLine("Display the even numbers:");
                foreach (int number in evenNumbers)
                {
                    Console.WriteLine(number);
                }
                Console.WriteLine("Display the odd numbers:");
                foreach (int number in oddNumbers)
                {
                    Console.WriteLine(number);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
    }
}

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