# Can any one help me for these basic programs [Resolved]

Posted by Shreedar under C# on 10/21/2015 | Points: 10 | Views : 1875 | Status : [Member] | Replies : 11
2.windows schedular program to send an automatic e-mail for a perticular time every day.
3.find a Nth highest or lowest number in a given series of numbers.
4.find how many times a char or word repeated in a given string.

Regards

Sridhar Thota.
www.dotnet-sridhar.blogspot.com

#### Responses

Posted by: Rajnilari2015 on: 10/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50
 0 Question #3: Find a Nth highest or lowest number in a given series of numbers. Solution 1: Use Min() and Max() Extension Methods of Enumerable Class that resides under the namespace System.Linq using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { List lstInts = new List() { 1, 206, 34, -4, 590, 61, 78, 8, 9, 180,237 }; int minNum = lstInts.Min(); //lowest number int maxNum = lstInts.Max(); //highest number Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum); Console.ReadKey(true); } } } Result Min = -4 , Max = 590 OR Since you asked for numbers which can be +ve,-ve, fractional (not considering Complex numbers here), you can modify the above program as under List lstNumbers= new List() { 1, 2.06, 34, -4, 5.90, 61.9, 7.8, 8, 9, 18.0,23.7 }; double minNum = lstNumbers.Min(); //lowest number double maxNum = lstNumbers.Max(); //highest number Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum); Hope this helps-- Thanks & Regards, RNA TeamShreedar, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Rajnilari2015 on: 10/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25
 0 Question #3: Find a Nth highest or lowest number in a given series of numbers. Solution 2: Use OrderBy for sorting and then apply the First() and Last() Extension Methods of Enumerable Class that resides under the namespace System.Linq using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //Question #3: Find a Nth highest or lowest number in a given series of numbers. List lstNumbers = new List() { 1, 2.06, 34, -4, 5.90, 61.9, 7.8, 8, 9, 18.0,23.7 }; var sortedNumbers = lstNumbers.OrderBy(o => o).ToList(); double minNum = sortedNumbers.First(); double maxNum = sortedNumbers.Last(); Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum); Console.ReadKey(true); } } } -- Thanks & Regards, RNA TeamShreedar, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Rajnilari2015 on: 10/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25
 0 Question #3: Find a Nth highest or lowest number in a given series of numbers. Solution 3: The Old school wayusing System;using System.Collections.Generic;namespace ConsoleApplication2{ class Program { static void Main(string[] args) {//Question #3: Find a Nth highest or lowest number in a given series of numbers.List lstNumbers = new List() { 1, 2.06, -36.4, -4, 5.90, 61.9, 79.8, 8, 9, 18.0,23.7 };double minNum = 0; double maxNum = 0;minNum = lstNumbers[0]; maxNum = lstNumbers[0];for(int i = 1;i< lstNumbers.Count;i++) { //logic for obtaining the Minimum Number if (minNum > lstNumbers[i]) minNum = lstNumbers[i];//logic for obtaining the Maximum Number if (maxNum < lstNumbers[i]) maxNum = lstNumbers[i]; } Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum);Console.ReadKey(true); } }} -- Thanks & Regards, RNA TeamShreedar, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25
 0 Question #3: Find a Nth highest or lowest number in a given series of numbers. Solution 4: Using Take and Single Extension methodsusing System;using System.Collections.Generic;using System.Linq;namespace ConsoleApplication2{ class Program { static void Main(string[] args) {//Question #3: Find a Nth highest or lowest number in a given series of numbers. List lstNumbers = new List() { 1, 2.06, -36.4, -4, 5.90, 61.9, 79.8, 8, 9, 18.0,23.7 }; double minNum = lstNumbers.OrderBy(o => o).Take(1).Single();double maxNum = lstNumbers.OrderByDescending(o => o).Take(1).Single();Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum); Console.ReadKey(true); } }} -- Thanks & Regards, RNA TeamShreedar, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25
 0 Question #3: Find a Nth highest or lowest number in a given series of numbers. Solution 5: Using indexing technique after sortingusing System;using System.Collections.Generic;using System.Linq;namespace ConsoleApplication2{ class Program { static void Main(string[] args) {//Question #3: Find a Nth highest or lowest number in a given series of numbers.List lstNumbers = new List() { 1, 2.06, -36.4, -4, 5.90, 618.9, 79.8, 8, 9, 18.0,23.7 };int itemsCount = lstNumbers.Count;var sortedList = lstNumbers.OrderBy(o => o).ToList();double minNum = sortedList[0];double maxNum = sortedList[itemsCount -1];Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum); } }}-- Thanks & Regards, RNA TeamShreedar, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25
 0 Question #4: Find how many times a word repeated in a given string. Solution 1: Using Group By and LINQ using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //Question #4: Find how many times a word repeated in a given string. List lstWords = new List() { "hello","this","is","dotnet","dotnet","funda","is","is"}; (from word in lstWords group word by word into g select new { Words = g.Key, WordsRepeated = (g.Count() - 1) == 0 ? String.Concat("Word ", g.Key, " has never repeated") : String.Concat("Word ", g.Key, " repeated ", g.Count(), " times") }).ToList().ForEach(i => Console.WriteLine("{0} : {1}", i.Words, i.WordsRepeated)); Console.ReadKey(true); } } } Result hello : Word hello has never repeated this : Word this has never repeated is : Word is repeated 3 times dotnet : Word dotnet repeated 2 times funda : Word funda has never repeated If you are looking for a words repeated in a single string, then you can split them by the delimiters and apply the same program as under using System; using System.Linq; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //Question #4: Find how many times a word repeated in a given string.. string lstWords = "hello this is dotnet funda . dotnet funda is a dotnet forum"; (from word in lstWords.Split(' ') group word by word into g select new { Words = g.Key, WordsRepeated = (g.Count() - 1) == 0 ? String.Concat("Word ", g.Key, " has never repeated") : String.Concat("Word ", g.Key, " repeated ", g.Count(), " times") }).ToList().ForEach(i => Console.WriteLine("{0} : {1}", i.Words, i.WordsRepeated)); Console.ReadKey(true); } } } -- Thanks & Regards, RNA TeamShreedar, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25
 0 Question #4: Find how many times a word repeated in a given string. Solution 2: Using Group By and LAMBDA using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //Question #4: Find how many times a word repeated in a given string. List lstWords = new List() { "hello","this","is","dotnet","dotnet","funda","is","is"}; lstWords .GroupBy(x => x) .Select(g => new { Words = g.Key, WordsRepeated = (g.Count() - 1) == 0 ? String.Concat("Word ", g.Key, " has never repeated") : String.Concat("Word ", g.Key, " repeated ", g.Count(), " times") }).ToList().ForEach(i => Console.WriteLine("{0} : {1}", i.Words, i.WordsRepeated)); Console.ReadKey(true); } } } If you are looking for a words repeated in a single string, then you can split them by the delimiters and apply the same program as under using System; using System.Linq; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //Question #4: Find how many times a word repeated in a given string.. string lstWords = "hello this is dotnet funda . dotnet funda is a dotnet forum"; (from word in lstWords.Split(' ') group word by word into g select new { Words = g.Key, WordsRepeated = (g.Count() - 1) == 0 ? String.Concat("Word ", g.Key, " has never repeated") : String.Concat("Word ", g.Key, " repeated ", g.Count(), " times") }).ToList().ForEach(i => Console.WriteLine("{0} : {1}", i.Words, i.WordsRepeated)); Console.ReadKey(true); } } } -- Thanks & Regards, RNA TeamShreedar, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25
 0 Question #4: Find how many times a char repeated in a given string. Solution : using System;using System.Linq;namespace ConsoleApplication2{ class Program { static void Main(string[] args) { //Question #4: Find how many times a char repeated in a given string.. string lstWords = "hello this is dotnet funda"; (from character in lstWords.ToCharArray().Where(x => !Char.IsWhiteSpace(x)) group character by character into g select new { Characters = g.Key, RepeatedCharacters = (g.Count() - 1) == 0 ? String.Concat("Character ", g.Key, " has never repeated") : String.Concat("Character ", g.Key, " repeated ", g.Count(), " times") }).ToList().ForEach(i => Console.WriteLine("{0} : {1}", i.Characters, i.RepeatedCharacters)); Console.ReadKey(true); } }} Observe that I am removing the WhiteSpaces . Final outputh : Character h repeated 2 timese : Character e repeated 2 timesl : Character l repeated 2 timeso : Character o repeated 2 timest : Character t repeated 3 timesi : Character i repeated 2 timess : Character s repeated 2 timesd : Character d repeated 2 timesn : Character n repeated 2 timesf : Character f has never repeatedu : Character u has never repeateda : Character a has never repeated -- Thanks & Regards, RNA TeamShreedar, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25