Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

triangle calculator

namespace TriAngle;

internal class MainClass : Program
{
        public static void Main()
        {
            var triangle = new Triangle();
            Triangle.Runner();
        }
    }
 
public class Program : TriangleItems
{
    protected class Triangle : TryExcept
    {
        private double A { get; set; }
        private double B { get; set; }
        private double C { get; set; }

        public void TriangleSides()
        {
              this.A = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("a is " + A);
            this.B = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("b is " + B);
            this.C = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("c is " + C);
        }
        
        public void Perimeter()
        {
            if (A + B > C || B + C > A || A + C > B)
            {
                var perimeter = A + B + C;
                Console.WriteLine("Triangle's perimeter is " + perimeter);
            }
            else if (A == 0 || B == 0 || C == 0)
            {
                Console.WriteLine("Triangle's side can't be zero");
            }
            else
            {
                Console.WriteLine("Sorry! But such triangle doesn't exist!");
            }
        }  
        
        public void Area()
        {
           if (A + B > C || B + C > A || A + C > B)
            {
                var s = (A + B + C) / 2;
                var area = Math.Sqrt(s * (s - A) * (s - B) * (s - C));
                Console.WriteLine("Triangle's area is  " + area);
            }
            else if (A == 0 || B == 0 || C == 0)
            {
                Console.WriteLine("Triangle's side can't be zero");
            }
            else
            {
                Console.WriteLine("Sorry! But such triangle doesn't exist!");
            }
        }
        
        public static DateTime Runner()
        {
            while (true)
            {
                Console.WriteLine("Welcome to Triangle's calculator!");
                Console.WriteLine("Press 1 for start!");

                var input1 = Convert.ToInt32(Console.ReadLine());

                if (input1 == 1)
                {
                    StartProgram = DateTime.Now;
                }
                else
                {
                    throw (new FormatException("You must start program with '1' "));
                }

                Area_try_Except();
                Perimeter_try_Except();

                Console.WriteLine("You can stop program press 2");
                input1 = Convert.ToInt32(Console.ReadLine());

                if (input1 == 2)
                {
                    StopProgram = DateTime.Now;
                }
                else
                {
                    throw (new FormatException("You must stop program with '2' "));
                }
            }
        }
    }
}
 
public class TryExcept : Program
{
    protected static void Area_try_Except()
    {
        try
        {
            var triangle = new Triangle();
            Console.WriteLine("Insert values for triangle's area operation : ");
            triangle.TriangleSides();
            triangle.Area();
        }
        catch
        {
            throw new FormatException("Looks like something went wrong.Triangle's side must be a number!");
        }
    }
  
    protected static void Perimeter_try_Except()
    {
        try
        {
            var triangle = new Triangle();
                Console.WriteLine("You can also calculate triangle's perimeter.It can be same values as before or different.It doesn't affect program functionality!");
                Console.WriteLine("Before calculate perimeter, please enter values again : ");
                triangle.TriangleSides();
                triangle.Perimeter();
        }
        catch
        {
            throw new FormatException("Looks like something went wrong.Triangle's side must be a number!");
        }
    }
}
 
public class TriangleItems
{
    protected static DateTime StartProgram { get; set; }
    protected static DateTime StopProgram { get; set; }

    public static DateTime _start()
    {
        StartProgram = DateTime.Now;
        return StartProgram;
    }

    public static DateTime _stop()
    {
        StopProgram = DateTime.Now;
        return StopProgram;
    }
}
Comment

Triangle Calculator

 namespace TriangleCalc; 
 public class Program
 {
     private double A { get; set; }
     private double B { get; set; }
     private double C { get; set; }
     public void Triangle()
     {
         try
         {
             this.A = Convert.ToDouble(Console.ReadLine());
             Console.WriteLine("a is " + A);
             this.B = Convert.ToDouble(Console.ReadLine());
             Console.WriteLine("b is " + B);
             this.C = Convert.ToDouble(Console.ReadLine());
             Console.WriteLine("c is " + C);
         }  catch
         {
             throw (new Exception("Triangle's side must be a number"));
         }
     }
     
     public void Area()
     {
             if (A + B > C && B + C > A && A + C >B )  
             {
                 var s  = (A+B+C)/2;
                 var result = Math.Sqrt(s * (s - A) * (s - B) * (s - C));
                 Console.WriteLine("The area is " + result );
             }
             else
             {
                 Console.WriteLine("Result is NaN");
             }
     }
     
     public void Perimeter()
     {
         if (A + B > C && A + C > B && B + C > A)
         {
             var perimeter = A + B + C;
             Console.WriteLine("The perimeter is " + perimeter);
         }
         else
         {
             Console.WriteLine("Such triangle doesn't exist");
         }
     } 
 }

public class Runner : Program
{
    public static void Main(string[] args)
    {
        while (true)
        {
            Console.WriteLine("1-Start
2-Stop");
            var input1 = Convert.ToInt32(Console.ReadLine());

            if (input1 == 1)
            {
                var startProgram = Triangleİtems.Start;
            }
            else
            {
                throw (new Exception("Program must start with 1"));
            }

            var triangle = new Program();
            triangle.Triangle();
            triangle.Area();
            triangle.Perimeter();

            Console.WriteLine("You can end program with 2");
            input1 = Convert.ToInt32(Console.ReadLine());

            if (input1 == 2)
            {
                var stopProgram = Triangleİtems.Stop;
            }

            else
            {
                throw (new Exception("Program must stop with 2"));
            }
        }
    }
}

public static class Triangleİtems
{
    public static DateTime Start;
    public static DateTime Stop;

    public static DateTime StartProgram()
    {
        Start = DateTime.Now;
        return Start;
    }

    public static DateTime StopProgram()
    {
        Stop = DateTime.Now;
        return Stop;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: iis services in asp.net 
Csharp :: if else c# 
Csharp :: how to remove from list from index c# 
Csharp :: link list in c# 
Csharp :: list contains type c# 
Csharp :: c# list empty 
Csharp :: assert.equal 
Csharp :: csharp compare characters 
Csharp :: wpf app transparent background with blurred image affect 
Csharp :: calculate textbox value c# 
Csharp :: jobject alternative in system.text.json 
Csharp :: json serialize object capitalization config 
Csharp :: dotnet core clickable row 
Csharp :: aspnet for loop 
Csharp :: c# yes no cancel dialog with icons 
Csharp :: print bitmap company logo c sharp 
Csharp :: gcm_sender_id convert text 
Csharp :: how to make a beep in c# 
Csharp :: c# multipthreading 
Csharp :: save string to file c# 
Csharp :: .net core 3 entity framework constraint code first image field 
Csharp :: How to create a gameobject by code 
Csharp :: unity mouse click 
Csharp :: how to get text color alpha unity 
Csharp :: c# break file into words 
Csharp :: uncapitalize string c# 
Csharp :: gegenstände bewegen c# 
Csharp :: c# check if pdf is protected without password 
Csharp :: c# timestamp 
Csharp :: c# silent execute exe 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =