1. Find Minimum, Maximum and Average value of an integer array.
2. Find Minimum and maximum even numbers of an integer array.
3. Find first 4 numbers from an integer array.
4. Find top 4 numbers from an integer array.
5. Find bottom 4 numbers from an integer array.
6. Find top 4 even numbers from an integer array.
7. Find bottom 4 odd numbers from an integer array.
8. Find numbers between top 4 and bottom 4 nuumbers in an integer array.
9. Sort an integer array and display.
using System;
using System.Linq;
namespace ConsoleLINQBasic
{
class Test
{
int[] numbers = { 3, 2, 6, 4, 8, 7, 43, 98, 1, 41, 33, 13, 45, 9, 12, 16, 20, 26 };
public void Question1()
{//1. Find Minimum, Maximum and Average value of an integer array.
Console.WriteLine(numbers.Min());
Console.WriteLine(numbers.Max());
Console.WriteLine(numbers.Average());
}
public void Question2()
{//2. Find Minimum and maximum even numbers of an integer array.
Console.WriteLine(numbers.Where(n=>n % 2 == 0).Min());
Console.WriteLine(numbers.Where(n=>n % 2 == 0).Max());
}
public void Question3()
{//3. Find first 4 numbers from an integer array.
var four = numbers.Take(4);
int i=0;
foreach (var item in four)
{
i++;
Console.WriteLine("Number{0} is {1}",i,item);
}
}
public void Question4()
{//4. Find top 4 numbers from an integer array.
var four = numbers.OrderByDescending(n => n).Take(4);
int i = 0;
foreach (var item in four)
{
i++;
Console.WriteLine("TopNumber{0} is {1}", i, item);
}
}
public void Question5()
{//5. Find bottom 4 numbers from an integer array.
var four = numbers.OrderBy(n => n).Take(4);
int i = 0;
foreach (var item in four)
{
i++;
Console.WriteLine("BottomNo.{0} is {1}", i, item);
}
}
public void Question6()
{//6. Find top 4 even numbers from an integer array.
var four = numbers.Where(n=>n%2==0).OrderByDescending(n => n).Take(4);
int i = 0;
foreach (var item in four)
{
i++;
Console.WriteLine("TopEven{0} is {1}", i, item);
}
}
public void Question7()
{//7. Find bottom 4 odd numbers from an integer array.
var four = numbers.Where(n => n % 2 != 0).OrderBy(n => n).Take(4);
int i = 0;
foreach (var item in four)
{
i++;
Console.WriteLine("BottomOdd{0} is {1}", i, item);
}
}
public void Question8()
{//8.Find numbers between top 4 and bottom 4 nuumbers in an integer array.
var four = numbers.OrderByDescending(n => n).Skip(4).OrderBy(n => n).Skip(4);
int i = 0;
foreach (var item in four)
{
i++;
Console.WriteLine("BetweenN{0} is {1}", i, item);
}
}
public void Question9()
{//9. Sort an integer array and display.
var data = numbers.OrderBy(n => n);
foreach (var item in data)
{
Console.Write("{0} ",item);
}
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.Question1();
t.Question2();
t.Question3();
t.Question4();
t.Question5();
t.Question6();
t.Question7();
t.Question8();
t.Question9();
Console.ReadKey();
}
}
}
Link of LINQ Extension Methods Examples Part2
OUTPUT
1
98
21.5
2
98
Number1 is 3
Number2 is 2
Number3 is 6
Number4 is 4
TopNumber1 is 98
TopNumber2 is 45
TopNumber3 is 43
TopNumber4 is 41
BottomNo.1 is 1
BottomNo.2 is 2
BottomNo.3 is 3
BottomNo.4 is 4
TopEven1 is 98
TopEven2 is 26
TopEven3 is 20
TopEven4 is 16
BottomOdd1 is 1
BottomOdd2 is 3
BottomOdd3 is 7
BottomOdd4 is 9
BetweenN1 is 6
BetweenN2 is 7
BetweenN3 is 8
BetweenN4 is 9
BetweenN5 is 12
BetweenN6 is 13
BetweenN7 is 16
BetweenN8 is 20
BetweenN9 is 26
BetweenN10 is 33
1 2 3 4 6 7 8 9 12 13 16 20 26 33 41 43 45 98
No comments:
Post a Comment