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.
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();
}
}
}
Comments
Leave a comment