107 325
Assignments Done
98.9%
Successfully Done
In March 2023

# 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
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.");
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);
}
}
}
}

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!