Help writing a c# program. Prompt the user for the length of three line segments
ID: 3703599 • Letter: H
Question
Help writing a c# program.
Prompt the user for the length of three line segments as integers. If the three lines could form a triangle, print the integers and a message indicating they form a triangle. Use a state-controlled loop to allow users to enter as many different combinations as they want.
HINT: Three sides can make a triangle if the sum of any two sides is greater than the third side. For instance, the sides {3, 4, 5} can make a triangle. However, the group {2, 3, 10} is not a valid triangle combination.
Explanation / Answer
using System;
namespace _3LineSegment
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
do
{
Console.WriteLine("Enter Three sides value to form a triangle:");
Console.Write("Enter the value for a:");
String Result1 = Console.ReadLine();
while (!Int32.TryParse(Result1, out a))
{
Console.WriteLine("Not a valid number, Input number only!");
Console.Write("Enter the value for a:");
Result1 = Console.ReadLine();
Console.WriteLine(Result1);
}
Console.Write("Enter the value for b:");
String Result2 = Console.ReadLine();
while (!Int32.TryParse(Result2, out b))
{
Console.WriteLine("Not a valid number, Input number only!");
Console.Write("Enter the value for b:");
Result2 = Console.ReadLine();
}
Console.Write("Enter the value for c:");
String Result3 = Console.ReadLine();
while (!Int32.TryParse(Result3, out c))
{
Console.WriteLine("Not a valid number, Input number only!");
Console.Write("Enter the value for c:");
Result3 = Console.ReadLine();
}
if ((a + b > c) && (a + c > b) && (b + c > a))
{
Console.WriteLine("Value of a: " + a);
Console.WriteLine("Value of b: " + b);
Console.WriteLine("Value of c: " + c);
Console.WriteLine("entered value can form a triangle");
}
else
{
Console.WriteLine("entered value can not form a triangle");
}
Console.Write("Do you want to continue (Y/N)?");
if(Console.ReadLine() == "n")
{
System.Environment.Exit(0);
}
} while (Console.ReadLine() != "Y");
Console.ReadKey();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.