Write a C# program that enters 5 elements to an array. Display all the odd numbers and even numbers separately.
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Input 5 elements in the array :");
const int n = 5;
int[] numbers = new int[n];
for (int i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i+1);
string str = Console.ReadLine();
numbers[i] = int.Parse(str);
}
Console.Write("Odd numbers: ");
for (int i = 0; i < n; i++)
{
if (numbers[i] % 2 == 0)
Console.Write("{0} ", numbers[i]);
}
Console.WriteLine();
Console.Write("Even numbers: ");
for (int i = 0; i < n; i++)
{
if (numbers[i] % 2 == 1)
Console.Write("{0} ", numbers[i]);
}
Console.WriteLine();
}
catch
{
Console.WriteLine("Bad number");
return;
}
}
}
}
Comments
Leave a comment