Skip to main content

C#: Program #52(Using Linq Extension Methods)

using System;
using System.Collections.Generic;
using System.Linq;

namespace Linqmethods2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> series = new List<int>();
            series.AddRange(Enumerable.Range(1, 20));
            var squares = series.Select(a=>a*a).ToList();
            foreach (int i in squares)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("The sum of the two");
            List<int> lone = new List<int>{1,5,6,3,2};
            List<int> ltwo = new List<int> {2,4,7,1,5};
            var sums = lone.Zip(ltwo,(x,y)=>x+y);
            foreach (int i in sums)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("All items of list one that are not in list two:");
            var val=lone.Except(ltwo).ToList();
            foreach (int i in val)
            {
                Console.WriteLine(i);
            }

            List<int> nos = new List<int> { 2, 2, 2, 2, 6, 6, 6, 4 };
            Console.WriteLine("The sum of the all items in the list");
            Console.WriteLine(nos.Aggregate((x,y)=>x+y));

            Console.WriteLine("Average is {0}",nos.AsQueryable().Average());
            Console.WriteLine("if all are even in list nos:{0}",nos.All(x=>x%2==0));
            Console.WriteLine("if any is odd in list:{0}",(nos.Any(x=>x%2!=0)));

            Console.WriteLine("All distinctive nos:");
            var no=nos.Distinct().ToList();
            foreach (int i in nos)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();

        }
    }
}

Comments

Popular posts from this blog

C#: Program #35 (Oop2)

using System; namespace OOP_CONSTRUCTORS { class Animals     {         private string name;         private string sound;         public const string shelter = "Aibo's Hangout";         public Animals()             : this("No Name", "No Sound") { }         public Animals(string name)             : this(name, "No Sound") { }         public Animals(string n, string s)         {             setName(n);             SoundCap = s;             Console.WriteLine(SoundCap);             Counter = 1;             Console.WriteLine(Count);         }         public void introduced()         {             Console.WriteLine("Voice of {0} sounds like {1}", name, sound);         }         public void setName(string n)         {             int i = 1;                foreach(char c in n)                {                    if (!char.IsLetter(c))                    {                        i = 0;                    }

C#: Program #24 (DoWhile Loop)

using System; namespace DoWhile {     class Program     {         static void Main(string[] args)         {             Random no = new Random();             int numbergenerated = no.Next(1,20);             int numberguessed;             int turns=0;             do             {                 if (turns != 0)                 {                     Console.WriteLine("Guessed Wrong Number!");                 }                 Console.WriteLine("Guess a number between 1 and 20");                 numberguessed = Convert.ToInt32(Console.ReadLine());                 turns++;             } while (numberguessed != numbergenerated);             Console.WriteLine("Correct you got it in {0} turns",turns);             Console.ReadKey();         }     } }