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.Globalization;
namespace App
{
class Program
{
static void Main(string[] args)
{
int[,] twoDimensionalArray = new int[4, 3];
Console.Write("Enter twelve numbers: ");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
twoDimensionalArray[i, j] = int.Parse(Console.ReadLine());
}
}
Console.Write("Odd numbers are: ");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
if (twoDimensionalArray[i, j] % 2 != 0)
{
Console.Write("{0} ", twoDimensionalArray[i, j]);
}
}
}
Console.Write("\nEven numbers are: ");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
if (twoDimensionalArray[i, j] % 2 == 0)
{
Console.Write("{0} ", twoDimensionalArray[i, j]);
}
}
}
Console.ReadLine();
}
}
}
Comments
Leave a comment