Skip to main content

Posts

Showing posts from October, 2017

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)

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

using System; using System.Collections.Generic; using System.Linq; namespace Linqmethods {     class Program     {         static void Main(string[] args)         {             List<int> tosses = new List<int>();             Random rndm = new Random();             int i=0;             while (i < 100)             {                 tosses.Add(rndm.Next(1,3));                 i++;             }             Console.WriteLine("HEAD:{0}",tosses.Where(x => x == 1).Count());             Console.WriteLine("TAIL:{0}", tosses.Where(x => x == 2).Count());             List<string> names = new List<string> { "Ali", "Akbar", "Bilal", "Burhan", "Cilist", "Casper", "Ellie", "Ethsham", "Furqan", "Fahad", "Ging"};             var name = names.Where(x=>x.StartsWith("B")).ToList();             foreach (string n

C#: Program #50(Using LAMDA)

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LambdaCode {     class Program     {         public delegate double Arithmetic(double val);         static void Main(string[] args)         {             Arithmetic obj = x => ++x;             Console.WriteLine(obj(5));             obj = x => --x;             Console.WriteLine(obj(6));             obj = x => x / 2;             Console.WriteLine(obj(7));             obj = x => x * 2;             Console.WriteLine(obj(8));             Console.WriteLine("The even numbers are:");             List<int> numbers = new List<int> {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};             var evenlist = numbers.Where(x => x % 2 == 0).ToList();             foreach(int no in evenlist)             {                 Console.WriteLine(no);             }             Console.WriteLine("The no between 2 and 5");             v

C#: Program #49(Delegates)

using System; namespace DelegatesCode {     class Program     {         static void Main(string[] args)         {             Arithmetic method1,method2,combine;             method1=new Arithmetic(add);             method2=new Arithmetic(subtract);             combine = method1 + method2;             combine(10,8);             Console.ReadKey();         }         public delegate void Arithmetic(double no1,double no2);         public static void add(double num1, double num2)         {             Console.WriteLine(num1+num2);         }         public static void subtract(double number1, double number2)         {             Console.WriteLine(number1 - number2);         }     } }

C#: Program #48 (Generic Struct)

using System; using System.Linq; namespace Generics1 {     class Program     {         static void Main(string[] args)         {             Rectangle<string> myobj = new Rectangle<string>("8", "6");             Console.WriteLine(myobj.Area());             Console.ReadKey();         }         public struct Rectangle<T>         {             private T length,width;             public T Width { get { return width; } set { width = value; } }             public T Length { get { return length; } set { length = value; } }         public Rectangle(T w, T l)         {             width = w;             length = l;         }         public string Area()         {             double no1=Convert.ToDouble(Width);             double no2=Convert.ToDouble(Length);             return Convert.ToString(no1*no2);         }         }     } }

C#: Program #47 (Generic Methods)

using System; using System.Collections.Generic; using System.Linq; namespace Generics1 {     class Arithmetic     {         public void Sum<T>(ref T no1, ref T no2)         {             double number1=Convert.ToDouble(no1);             double number2 = Convert.ToDouble(no2);             Console.WriteLine(number1+number2);         }     } class Program     {         static void Main(string[] args)         {             Arithmetic obj = new Arithmetic();             int x = 1;             int y = 2;             obj.Sum(ref x,ref y);             string sx="6";             string sy="5";             obj.Sum(ref sx, ref sy);             Console.ReadKey();         }     } }

C#: Program #46 (Generic Collections)

using System; using System.Collections.Generic; using System.Linq; namespace Generics1 {     class Program     {         static void Main(string[] args)         {             List<Animal> objects=new List<Animal>();             objects.Add(new Animal("Hachico"));             objects.Add(new Animal("Krypto"));             objects.Add(new Animal("Balto"));             objects.Add(new Animal("Gray"));             objects.Insert(0,new Animal("Alex"));             Console.WriteLine("The number of items in List is:{0}",objects.Count());             foreach(Animal o in objects)             {                 Console.WriteLine(o.Name);             }             objects.RemoveAt(4);             Console.WriteLine("After Removal");             foreach (Animal o in objects)             {                 Console.WriteLine(o.Name);             }             Console.ReadKey();         }    

C#: Program #45 (Stack)

using System; using System.Collections; namespace StackCode {     class Program     {         static void Main(string[] args)         {             Stack obj = new Stack();             obj.Push(56);             obj.Push(78);             obj.Push(99);             obj.Push(44);             Console.WriteLine("What is on the top of the stack {0}",obj.Peek());             Console.WriteLine("Is 45 part of the stack",obj.Contains(45));             Console.WriteLine(obj.Pop());             Console.WriteLine("The stack contains:");             foreach (object o in obj)             {                 Console.WriteLine(o);             }             Console.ReadKey();         }     } }

C#: Program #44 (Queue)

using System; using System.Collections; namespace QueuesCode {     class Program     {         static void Main(string[] args)         {             Queue line = new Queue();             line.Enqueue(1);             line.Enqueue(2);             line.Enqueue(3);             line.Enqueue(4);             line.Enqueue(5);             foreach(object o in line)             {                 Console.WriteLine("The queue contains {0}",o);             }             Console.WriteLine("The queue contains 4: {0}",line.Contains(4));             line.Dequeue();             foreach (object o in line)             {                 Console.WriteLine("The queue contains {0}", o);             }             Console.WriteLine("The Peek is {0}", line.Peek());             object[] myarr = line.ToArray();             Console.WriteLine(String.Join(",", myarr));             Console.ReadKey();         }     } }

C#: Program #43 (Dictionary)

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DictionaryCode {     class Program     {         static void Main(string[] args)         {             string name;             Dictionary<string, string> FictionalHeroes = new Dictionary<string, string>();             FictionalHeroes.Add("Barry Allen","Flash");             FictionalHeroes.Add("Bruse Wayne", "Batman");             FictionalHeroes.Add("Oliver Queen", "Green Arrow");             FictionalHeroes.Add("Saitama", "One Punch man");             FictionalHeroes.Add("Kent Clark", "Superman");             foreach (KeyValuePair<string,string> o in FictionalHeroes)             {                 Console.WriteLine("{0} is the {1}",o.Key,o.Value);             }             FictionalHeroes.Remove("Kent Clark");             Console.Wr

C#: Program #42 (ArrayList)

using System; using System.Collections; namespace ArrayListCode {     class Program     {         static void Main(string[] args)         {             #region ArrayList code starts here             ArrayList mylist = new ArrayList();             mylist.Add(7);             mylist.Add(1);             mylist.Add(0);             mylist.Add(3);             mylist.Add(23);             mylist.Add(35);             Console.WriteLine("Mylist has {0} no of items",mylist.Count);             Console.WriteLine("Mylist has capacity of {0}", mylist.Capacity);             mylist.Sort();             Console.WriteLine("The Array contains");             foreach(object o in mylist)             {                 Console.WriteLine(o);             }             mylist.Reverse();             Console.WriteLine("The Array contains");             foreach (object o in mylist)             {                 Console.WriteLine(o);             }

C#: Program #41 (Oop6)

using System; namespace Oop6 {     class Program     {         static void Main(string[] args)         {             Vehicle Civic = new Vehicle(4,200,"Dinkeee");             if (Civic is IDrivable)             {                 Civic.Move();                 Civic.Stop();             }             else             {                 Console.WriteLine("The {0} Cant be driven", Civic.Brand);             }             Console.ReadKey();         }     } interface IDrivable     {         int Wheels { get; set;}         double Speed { get; set; }         void Move();         void Stop();     }  class Vehicle:IDrivable         {         public string Brand { get; set;}         public int Wheels{get; set;}         public double Speed{get; set;}         public Vehicle(int wheels=0,double speed=0,string brand="No Brand")         {             Brand = brand;             Wheels = wheels;             Speed = speed;         }    

C#: Program #40 (Oop5)

using System; namespace OOP5 {     class Program     {         static void Main(string[] args)         {             Shape[] shapes = {new Square(4),new Circle(4),new RECTANGLE(4,3)};             foreach (Shape s in shapes)             {                 s.Getinformation();                 Console.WriteLine("{0} has area of {1:f2}",s.Name,s.Area());                 Circle objcircle = s as Circle;                 if (objcircle == null)                 {                     Console.WriteLine("This is not a circle");                 }                 if(s is Circle)                 {                     Console.WriteLine("This is a circle");                 }                 object sq=new Square(5);                 Square sq1 = (Square)sq;                 Console.WriteLine("{0} has area of {1:f2}", sq1.Name, sq1.Area());                             }             Console.ReadKey();         }     }  abstract class

C#: Program #39 (Coin Toss)

using System; namespace Coin_Toss {     class Program     {         static void Main(string[] args)                 {             int minbound,maxbound;             Console.WriteLine("Player one give a lower limit");             minbound = Convert.ToInt32(Console.ReadLine());             Console.WriteLine("Player two give a upper limit");             maxbound = Convert.ToInt32(Console.ReadLine());             Generate_Numbers toss = new Generate_Numbers(minbound,maxbound);             Decide_Side obj = new Decide_Side(toss);             Console.ReadKey();         }     } class Generate_Numbers     {         int number,min,max;         public int Num { get { return number; } set { number = value; } }         public int MinRange{ get { return min; } set { min = value; } }         public int MaxRange { get { return max; } set { max = value; } }         Random obj = new Random();         public Generate_Numbers(int mi,int ma)         {  

C#: Program #38 (Oop4)

using System; namespace Oop_Inheritance_ {     class Program     {         static void Main(string[] args)         {             Animal obj=new Animal("Balto","Howl");             obj.setAnimalIdInfo(1,"Balto");             obj.getAnimalInfo();             Dog obj2 = new Dog("Hachico", "howl", "Grrrr");             obj2.Sound="Woof Woof";             obj2.setAnimalIdInfo(5, "Hachico");             obj2.getAnimalInfo();             Console.WriteLine(obj2.Name);             Console.WriteLine(obj2.Sound);             Animal sloth = new Dog("Simon", "bark", "woof");             sloth.MakeSound();             Console.ReadKey();         }  class Dog:Animal     {         string sound2;         public string Sound2 {get {return sound2;} set{sound2=value;}}         public void MakeSound()         {             Console.WriteLine("{0} says {1} and {2}&

C#: Program #37 (Oop4)

using System; namespace oop4 { class Person     {                 static void Main(string[] args)         {             human obj = new human();             obj.Name="Bill";             human.Inner obj2 = new human.Inner(5,8,60);             Console.WriteLine(obj2.Status());             Console.ReadKey();         }     }     class human     {         string name;         public string Name { set { name = value; } get { return name; } }         public class Inner         {             string healthstatus;             int heightinfoot = 0;             int heightininches = 0;             int weight = 0;            public Inner(int hf,int hi,int w)             {                 heightinfoot = hf;                 heightininches = hi;                 weight = w;                             }            public string Status()             {                     if (heightinfoot == 4 && heightininches == 6)                     {        

C#: Program #36 (Oop3)

using System; namespace Oop_Inheritance_ {     class Program     {         static void Main(string[] args)         {             Animal obj=new Animal("Balto","Howl");             obj.setAnimalIdInfo(1,"Balto");             obj.getAnimalInfo();             Dog obj2 = new Dog("Hachico", "howl", "Grrrr");             obj2.Sound="Woof Woof";             obj2.setAnimalIdInfo(5, "Hachico");             obj2.getAnimalInfo();             Console.WriteLine(obj2.Name);             Console.WriteLine(obj2.Sound);             Console.ReadKey();         }           }  class Animal     {        protected string name;        protected string sound;        protected AnimalIDInfo objid = new AnimalIDInfo();        public void setAnimalIdInfo(int id, string own)        {            objid.IDNum = id;            objid.Owner = own;        }        public void getAnimalInfo()        {        

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 #34 (Oop)

using System; namespace Oop {     public class Animal     {         string name;         string sound;         static int countanimals=0;         public Animal()         {             name = "No name assigned";             sound = "null";             countanimals++;         }         public void setName(string n)         {             name = n;         }         public void getName()         {             Console.WriteLine("Name:{0}", name);         }         public void setSound(string s)         {             sound = s;         }         public void getSound()         {             Console.WriteLine("Sound:{0}", sound);         }         public static void countAnimals()         {             Console.WriteLine("Number of Animals are:{0}", countanimals);         }     }     class Program     {         static void Main(string[] args)         {             Animal dog = new Animal();             d

C#: Program #33 (Structures)

using System; namespace Structure {     class Program     {         static void Main(string[] args)         {            triangle tri1, tri2;            tri1.breadth = 2;            tri1.height = 3;            tri1.area = 4;            tri1.areaCal();            tri2.breadth = 0;            tri2.height = 0;            tri2.area = 0;            tri2.areaCal(5.2f, 4.4f);            Console.ReadKey();         }         struct triangle         {             public float breadth;             public float height;             public float area;            public triangle(float b,float l,float a)             {                 breadth =b;                 height = l;                 area = a;             }             public void areaCal()             {                 area = 0.5f*(breadth * height);                 Console.WriteLine("The area of the triangle is {0}",area);             }             public void areaCal(float l,float b)          

C#: Program #32 (Methods7)

using System; namespace Methods7 {     class Program     {                 static void Main(string[] args)         {             Status(state.Off);             Console.ReadKey();         }         enum state:byte         {            Off=0,            On,         }         static void Status(state s)         {             Console.WriteLine("The machine is in {0} state whose code is {1}",s,(byte)s);         }     } }

C#: Program #31 (Methods6)

using System; namespace Methods6 {     class Program     {         static void Main(string[] args)         {             Console.WriteLine("The sum of 5.4+7.0 is:{0}",sum(5.4,7.0));             Console.WriteLine("The sum of 5+7 is:{0}", sum(5, 7));             Console.ReadKey();         }         static double sum(double a,double b)         {             return a + b;         }         static double sum(int a,int b)         {             return a + b;         }     } }

C#: Program #30 (Methods5)

using System; namespace Methods5 {     class Program     {         static void Main(string[] args)         {             Console.WriteLine("Enter your name,age and gpa");             string Whatareyoucalled = Console.ReadLine();             int howOld = Convert.ToInt32(Console.ReadLine());             double acheivement = Convert.ToDouble(Console.ReadLine());             information(name: Whatareyoucalled, age : howOld,gpa:acheivement);             Console.ReadKey();         }         static void information(int age, string name, double gpa)         {             Console.WriteLine("Name:{0} \nAge:{1} \nGPA:{2}",name,age,gpa);         }     } }

C#: Program #29 (Methods4)

using System; namespace Methods4 {     class Program     {         static void Main(string[] args)         {             Console.WriteLine(Sumission(1,2,3,4,5,6));             Console.ReadKey();         }         static double Sumission(params int[] arguments)         {             int sum=0;             foreach(int i in arguments)             {                 sum += i;             }             return sum;         }     } }

C#: Program #28 (Methods3)

using System; namespace Methods3 {     class Program     {         static void Main()         {             int x = 5, y = 6;             Console.WriteLine("Value of x is {0} and y is {1}", x, y);             exchange(ref x,ref y);             Console.WriteLine("Value of x is {0} and y is {1}", x, y);             Console.ReadKey();         }         static void exchange(ref int a, ref int b)         {             int temp;                       temp = a;             a = b;             b = temp;                   }     } }

C#: Program #27 (Methods2)

using System; namespace Methods2 {     class Program     {         static void Main(string[] args)         {             int solution=5;             cube(solution,out solution);             Console.WriteLine("Solution is {0}",solution);             Console.ReadKey();         }         static void cube(int a,out int s)         {             s = a * a * a;         }     } }

C#: Program #26 (Methods1)

using System; namespace Methods1 {     class Program     {         static void Main()         {             int x = 5, y = 6;             exchange(x,y);             Console.ReadKey();         }         static void exchange(int a,int b)         {             int temp;             Console.WriteLine("Value of a is {0} and b is {1}",a,b);             temp = a;             a = b;             b = temp;             Console.WriteLine("Value of a is {0} and b is {1}", a, b);         }     } }

C#: Program #25 (Exceptional Handling)

using System; namespace Exceptional_Handling {     class Program     {         static void Main(string[] args)         {             int a = 10;             int b = 0;             try             {                 Console.WriteLine("10/0 is:{0}", Division(a, b));             }             catch (DivideByZeroException ex)             {                 Console.WriteLine("Division by zero leads to infinity which is undefined and your values throws {0} because you {1}", ex.GetType().Name, ex.Message);             }             finally             {                 Console.WriteLine("Subscribe my youtube Channel Gray Frost");             }             Console.ReadKey();         }         static int Division(int numerator,int denominator)         {             if (denominator == 0)             {                 throw new System.DivideByZeroException();             }                             return (numerator / denominator)

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();         }     } }

C#: Program #23 (While Loop)

using System; namespace While_Loop {     class Program     {         static void Main(string[] args)         {             int i=1;             while (i < 10)             {                 if (i%2==0)                 {                     i++;                    continue;                 }                 Console.WriteLine("Odd number found:{0}",i);                 i++;             }             Console.ReadKey();         }     } }

C#: Program #22 (Conditional Statements 2)

using System; namespace Conditional_Statements2 {     class Program     {         static void Main(string[] args)         {             int age;             Console.WriteLine("Enter Your Age");             age = Convert.ToInt32(Console.ReadLine());             switch (age)             {                     case 1:                     case 2:                     Console.WriteLine("Go to Day Care");                     break;                     case 3:                     case 4:                     case 5:                     case 6:                     case 7:                     case 8:                     case 9:                     Console.WriteLine("Go to Primary School");                     break;                     case 10:                     case 11:                     case 12:                     case 13:                     case 14:                     case 15:                     case 16:                  

C#: Program #21 (Conditional Statements 1)

using System; namespace Conditional_Statements {     class Program     {         static void Main(string[] args)         {             int numbers;             Console.WriteLine("Wnter Your Percentage");             numbers=Convert.ToInt32(Console.ReadLine());             if (numbers < 40)             {                 Console.WriteLine("You failed");             }             else if ((numbers >= 40) && (numbers < 50))             {                 Console.WriteLine("You got E grade");             }             else if ((numbers >= 50) && (numbers < 60))             {                 Console.WriteLine("You got D grade");             }             else if ((numbers >= 60) && (numbers < 70))             {                 Console.WriteLine("You got C grade");             }             else if ((numbers >=70) && (numbers < 80))             {