Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Find the nth Maximum Grade Point achieved by the students from the list of student

C# Sharp LINQ : Exercise-14 with Solution

Write a program in C# Sharp to find the n-th Maximum grade point achieved by the students from the list of students.

Sample Solution:-

C# Sharp Code:

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

public class Students  
    {  
        public string StuName { get; set; }  
        public int GrPoint { get; set; }  
        public int StuId { get; set; }  
  
        public List<Students> GtStuRec()  
        {  
            List<Students> stulist = new List<Students>();  
            stulist.Add(new Students { StuId = 1, StuName = " Joseph ", GrPoint = 800 });  
            stulist.Add(new Students { StuId = 2, StuName = "Alex", GrPoint = 458 });  
            stulist.Add(new Students { StuId = 3, StuName = "Harris", GrPoint = 900 });  
            stulist.Add(new Students { StuId = 4, StuName = "Taylor", GrPoint = 900 });  
            stulist.Add(new Students { StuId = 5, StuName = "Smith", GrPoint = 458 });  
            stulist.Add(new Students { StuId = 6, StuName = "Natasa", GrPoint = 700 });  
            stulist.Add(new Students { StuId = 7, StuName = "David", GrPoint = 750 });  
            stulist.Add(new Students { StuId = 8, StuName = "Harry", GrPoint = 700 });  
            stulist.Add(new Students { StuId = 9, StuName = "Nicolash", GrPoint = 597 });  
            stulist.Add(new Students { StuId = 10, StuName = "Jenny", GrPoint = 750 });  
            return stulist;  
        }  
    }  
    class LinqExercise14
    {  
        static void Main(string[] args)  
        {  
            Students e= new Students();  
			
			
            Console.Write("\nLINQ : Find the nth Maximum Grade Point achieved by the students from the list of student : "); 
            Console.Write("\n------------------------------------------------------------------------------------------\n");	

		    Console.Write("Which maximum grade point(1st, 2nd, 3rd, ...) you want to find  : ");
		    int grPointRank = Convert.ToInt32(Console.ReadLine()); 			
			Console.Write("\n");
            var stulist = e.GtStuRec();  
            var students = (from stuMast in stulist  
                 group stuMast by stuMast.GrPoint into g  
                 orderby g.Key descending   
                 select new  
                     {  
                         StuRecord = g.ToList()                                      
                     }).ToList();  
  
            students[grPointRank - 1].StuRecord  
                .ForEach(i => Console.WriteLine(" Id : {0},  Name : {1},  achieved Grade Point : {2}",i.StuId, i.StuName, i.GrPoint));  
  
            Console.ReadKey();  
        }  
    }
	

Sample Output:

LINQ : Find the nth Maximum Grade Point achieved by the students fro
m the list of student :                                             
--------------------------------------------------------------------
----------------------                                              
Which maximum grade point(1st, 2nd, 3rd, ...) you want to find  : 3 
                                                                    
 Id : 7,  Name : David,  achieved Grade Point : 750                 
 Id : 10,  Name : Jenny,  achieved Grade Point : 750 
 

Flowchart:

Flowchart: LINQ : Find the nth Maximum Grade Point achieved by the students from the list of student

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to convert a string array to a string.
Next: Write a program in C# Program to Count File Extensions and Group it using LINQ.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.