Q:

Write a program in C# Sharp to display the top n-th records

0

 Write a program in C# Sharp to display the top n-th records. 
Test Data :
The members of the list are :
5
7
13
24
6
9
8
7
How many records you want to display ? : 3
Expected Output :
The top 3 records from the list are :
24
13
9

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
class LinqExercise11 
    {  
        static void Main(string[] args)  
      {  
        List templist = new List();
        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);
            }
        }  
    }

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now