Determining triangle type based on sides length in C#

Our task is to determine what kind of triangle it is based on it’s side’s length. Before we start let’s understand definitions of triangles:

Equilateral: All the side are equal in size.

Isosceles: Any two sized are equal.

Algorithm: We’ll group our sides and if group count is 1 that means triangle is EQUILATERAL. If group count is 2 that means it’s ISOSCELES. If group count is greater than 2 that means NONE OF THESE.

Code in C#

        static void Main(string[] args)
        {
            int N = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < N; ++i)
            {
                string[] input = Console.ReadLine().Split(' ');
                TrianleType(input);
            }
            Console.ReadKey();
        }

        private static void TrianleType(string[] input)
        {
            int count = input.GroupBy(x => x).Count();
            if (count == 1)
            {
                Console.WriteLine("Equilateral");
            }
            else if (count == 2)
            {
                Console.WriteLine("Isosceles");
            }
            else 
            {
                Console.WriteLine("None of these");
            }
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s