Odd and Even number in c#
if you are an software engineering student finding the odd and even number will be your first coding so let me break it to you then we share the code together
a number is even when number%2=0 .
for example 10%2=0 so its an even number .
this % is mod option, not division
the code i wrote was in first semester it will get some numbers and choose
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
namespace zojfard
{
class Program
{
static void Main(string[] args)
{
int k;
Console.WriteLine("enter the amount of numbers you like to do ");
int i;
i=Convert.ToInt32(Console.ReadLine());
ArrayList odd = new ArrayList(i);
ArrayList even = new ArrayList(i);
int[] n= new int[i];
Console.WriteLine("enter digits one by one");
for (int j = 0; j <i;j++)
{
n[j] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("numbers have been entered");
for (int j = 0; j <i; j++)
{
Console.WriteLine("{0,6:G}",n[j]);
}
for( k=0; k<i; k++)
{
if (n[k] % 2 == 0)
{
even.Add(n[k]);
}
else
{
odd.Add(n[k]);
}
}
Console.WriteLine("these are even");
foreach (int h in even)
{
Console.WriteLine(h);
}
Console.WriteLine("these are odd");
foreach (int g in odd)
{
Console.WriteLine(g);
}
Console.ReadKey();
}
}
a number is even when number%2=0 .
for example 10%2=0 so its an even number .
this % is mod option, not division
the code i wrote was in first semester it will get some numbers and choose
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
namespace zojfard
{
class Program
{
static void Main(string[] args)
{
int k;
Console.WriteLine("enter the amount of numbers you like to do ");
int i;
i=Convert.ToInt32(Console.ReadLine());
ArrayList odd = new ArrayList(i);
ArrayList even = new ArrayList(i);
int[] n= new int[i];
Console.WriteLine("enter digits one by one");
for (int j = 0; j <i;j++)
{
n[j] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("numbers have been entered");
for (int j = 0; j <i; j++)
{
Console.WriteLine("{0,6:G}",n[j]);
}
for( k=0; k<i; k++)
{
if (n[k] % 2 == 0)
{
even.Add(n[k]);
}
else
{
odd.Add(n[k]);
}
}
Console.WriteLine("these are even");
foreach (int h in even)
{
Console.WriteLine(h);
}
Console.WriteLine("these are odd");
foreach (int g in odd)
{
Console.WriteLine(g);
}
Console.ReadKey();
}
}
Comments
Post a Comment