Machine Problem 5.8.
Write a program using two-‐dimensional arrays that lists the Odd numbers and Even numbers separately in a given 12 input values.
Sample input/out dialogue:
Enter twelve numbers: 15 20 13 35 40 16 18 20 18 20 19
Odd numbers are: 15 13 35 19
Even numbers are: 40 16 18 20 18 20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int[,] array = new int[2, 12];
int oddCounter = 0;
int evenCounter = 0;
Console.Write("Enter twelve numbers: ");
string[] tmpArr = Console.ReadLine().Split(' ');
for (int i = 0; i < tmpArr.Length; i++)
{
int num = Convert.ToInt32(tmpArr[i]);
if (num % 2 != 0)
{
array[0, oddCounter] = num;
oddCounter++;
}
else
{
array[1, evenCounter] = num;
evenCounter++;
}
}
Console.Write("Odd numbers are: ");
for (int i = 0; i < oddCounter; i++)
{
Console.Write(array[0, i] + " ");
}
Console.WriteLine();
Console.Write("Even numbers are: ");
for (int i = 0; i < evenCounter; i++)
{
{
Console.Write(array[1, i] + " ");
}
}
Console.ReadKey();
}
}
}
Comments
Leave a comment