write a C# program that prompts for 3 positive integer inputs and then check (a)
ID: 3594619 • Letter: W
Question
write a C# program that prompts for 3 positive integer inputs and then check (a) if these 3 form a triangle, (b) if they form a triangle, what kind of triangle is that: right angle triangle, acute triangle (all three angles < 90 degrees), or obtuse triangle (one of the three angles is greater than 90 degrees), (c) if they form an isosceles triangle (i.e., two sides are equal), or equilateral triangle (all three sides are equal).
Test your program with triplets like (3, 4, 5), (1, 1, 1), and (1, 1, 2) etc.
Test your program with triplets like (3, 4, 5), (1, 1, 1), and (1, 1, 2) etc.
Explanation / Answer
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter first integer: ");
int i = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter second integer: ");
int j = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter third integer: ");
int k = Convert.ToInt32(Console.ReadLine());
if((i+j)>k && (j+k)>i && (k+i)>j){
Console.WriteLine("Integers can form a triangle");
int max = Math.Max(i, Math.Max(j, k));
int min = Math.Min(i, Math.Min(j, k));
int mid = (i+j+k)-(max+min);
if((max*max) == (min*min + mid*mid)){
Console.WriteLine("Integers form a right-angled triangle");
}
else if((max*max) < (min*min + mid*mid)){
Console.WriteLine("Integers form a acute-angled triangle");
}
else if((max*max) > (min*min + mid*mid)){
Console.WriteLine("Integers form a acute-angled triangle");
}
if(i==j && j==k){
Console.WriteLine("Integers form a equilateral triangle");
}
else if(i==j || j==k || i==k){
Console.WriteLine("Integers form a isosceles triangle");
}
else{
Console.WriteLine("Triangle formed in neither equilaterl not isosceles");
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.