'I am new here as well as into coding ! I solved a problem but i was wondering if the same problem could be solved in different/shorter ways?

The Problem:
Imagine you are a developer and get a job in which you need to create a program for a teacher. He needs a program written in c# that calculates the average score of his students. So he wants to be able to enter each score individually and then get the final average score once he enters -1. So the tool should check if the entry is a number and should add that to the sum. Finally once he is done entering scores, the program should write onto the console what the average score is. The numbers entered should only be between 0 and 20. Make sure the program doesn't crash if the teacher enters an incorrect value. Test your program thoroughly.

My solution to the problem :

static void Main(string[] args)
    {
        int digit = 0,sum=0,counter=0;
        string x;
        try
        {
            for (int i = 0; i <= counter; i++)
            {
                Console.WriteLine("Please Enter Score");
                x = Console.ReadLine();
                bool isParsable = Int32.TryParse(x,out digit);
                if (isParsable)
                {
                    if (digit >= 0 && digit <= 20)
                    {
                        Console.WriteLine("Valid Number");
                        sum += digit;
                        counter++;
                        Console.WriteLine($"Student number {counter} got {digit}");
                    }
                    else if (digit == -1)
                    {
                        Console.WriteLine($"Total sum is {sum}");
                        Console.WriteLine($"Total number of students is {counter}");
                        Console.WriteLine($"Average score of {counter} students is {sum / counter}");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Please enter a valid score");
                    }
                }
                Console.WriteLine("Please enter Numerical Values only");
            }
        }
        catch (DivideByZeroException)
        {
             Console.WriteLine("Unable to get results");
        }
        
    } 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source