C# Sharp Exercises: Display top nth records from the list
C# Sharp LINQ : Exercise-11 with Solution
Write a program in C# Sharp to display the top nth records.
Sample Solution:-
C# Sharp Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class LinqExercise11
{
static void Main(string[] args)
{
List<int> templist = new List<int>();
templist.Add(5);
templist.Add(7);
templist.Add(13);
templist.Add(24);
templist.Add(6);
templist.Add(9);
templist.Add(8);
templist.Add(7);
Console.Write("\nLINQ : Display top nth records from the list : ");
Console.Write("\n----------------------------------------------\n");
Console.WriteLine("\nThe members of the list are : ");
foreach (var lstnum in templist)
{
Console.WriteLine(lstnum+" ");
}
Console.Write("How many records you want to display ? : ");
int n= Convert.ToInt32(Console.ReadLine());
templist.Sort();
templist.Reverse();
Console.Write("The top {0} records from the list are : \n",n);
foreach (int topn in templist.Take(n))
{
Console.WriteLine(topn);
}
}
}
Sample Output:
---------------------------------------------- The members of the list are : 5 7 13 24 6 9 8 7 How many records you want to display ? : 5 The top 5 records from the list are : 24 13 9 8 7
Pictorial Presentation:
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to Accept the members of a list through the keyboard and display the members more than a specific value.
Next: Write a program in C# Sharp to find the uppercase words in a string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework