Problem 1-6

CSharp · Console

/Program.cs

// Determine a type of triangle by the length of its sides a, b, c.
// The program should input positive integers a, b, c and print 
// “rectangular”, “acute”, “obtuse”, or “impossible” accordingly.

using System;
using System.Diagnostics;
					
public class Program
{
	public static int InputInt(string prompt)
	{
		Console.Write("Enter " + prompt + ":");
		return Convert.ToInt32(Console.ReadLine());
	}
	
	public static void Main()
	{
		// input data
		int a = InputInt("length of side a");
		int b = InputInt("length of side b");
		int c = InputInt("length of side c");
		Trace.Assert(a > 0 && b > 0 && c > 0);
		
		if(c < a)
		{	// swap c,a; c increases
			int t = c; c = a; a = t;
		}
		/* original sides stored in a,b,c and c >= a  */

		if(c < b)
		{	// swap c,b; c increases
			int t = c; c = b; b = t;
		}
		/* original sides stored in a,b,c and c >= a and c>=b */
		
		// calculate
		if (a + b <= c){
			Console.Write("The sides cannot create a triangle!");	
		}else if (a * a + b * b > c * c) {
			Console.Write("It is an acute triangle");
		}else if (a * a + b * b == c * c) {
			Console.Write("It is a right triangle");
		}else{
			Console.Write("It is an obtuse triangle");
		}
	}
}
;