0
votes

I am making a code where the user is asked, "How many marks are there?" and then they put in the number of marks they mentioned. After that, it prints out the maximum mark and the minimum mark.

            int MarkNumber;
            Console.Write("How many marks are there?: ");
            MarkNumber = Convert.ToInt32(Console.ReadLine());

            for (int SchoolM = 0; SchoolM < MarkNumber; SchoolM++)
            { 
                Console.Write("Input a Mark: ");
                Console.ReadLine();                   
            }

I am not the best at coding so I have no direction of how to find the max and min in a loop. I got up to the part where they are able to input the marks, but I am unsure of how to find the max and the min. I have looked up how to do the max and min, but it usually shows up for finding the max and min in arrays and that is not what I desire to do.

1
Before the loop: int currentMark; int min = 0; int max = 0;. Inside the loop: currentMark = Convert.ToInt32(Console.ReadLine()); if (currentMark > max) max = currentMark; else if (currentMark < min) min = currentMark;. Alternatively, you could add the marks to a list and calculate the min and max afterward.41686d6564
Side note: when dealing with user input, always prefer int.TryParse() over Convert.ToInt32().41686d6564
Oh alright, I would use other alternatives, but I am still inexperienced. Thank you!KyuRiii

1 Answers

0
votes

You should be use variables one for max number and other for lower number

Maybe something like it

 static void Main(string[] args) {

    {
        int MarkNumber;
        int iminor = 0;
        int imax = 0;
        Console.Write("How many marks are there?: ");
        MarkNumber = Convert.ToInt32(Console.ReadLine());

        for (int SchoolM = 0; SchoolM < MarkNumber; SchoolM++)
        { 
            Console.Write("Input a Mark: ");
            var mark = Convert.ToInt32(Console.ReadLine());        
            if (mark > imax)  imax = mark;
            if (mark < iminor) iminor = mark;
        }
        Console.WriteLine($" Minor Mark {iminor} Max Mark {imax}");
    }