C# – Object Oriented Programming – Lab 2 (Equation of a Line)

The equation of a line is given as: y = mx +c. Write a program that asks the user for the start and end points of a line and calculates values for m and c:

m = The slope of the line (y2 – y1) / (x2 – x1)
c = The intercept (where the line intersects the y axis (i.e. Where x = 0)

Here’s my solution for it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab1Pt2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "The Console";
            Console.WriteLine("The equation of a line is given as: y = mx + c");
            Console.WriteLine();
            Console.WriteLine("Provide start and end points to calculate:");
            Console.WriteLine("m - the slope of the line");
            Console.WriteLine("c - the point where the line intercepts the y-axis");
            Console.WriteLine();

            Console.Write("What is the value of x1 = ");
            string x1Input = Console.ReadLine();
            float x1 = float.Parse(x1Input);

            Console.Write("What is the value of y1 = ");
            string y1Input = Console.ReadLine();
            float y1 = float.Parse(y1Input);

            Console.Write("What is the value of x2 = ");
            string x2Input = Console.ReadLine();
            float x2 = float.Parse(x2Input);

            Console.Write("What is the value of y2 = ");
            string y2Input = Console.ReadLine();
            float y2 = float.Parse(y2Input);

            Console.WriteLine();
            Console.WriteLine("So....");
            Console.WriteLine("(x1,y1) = (" + x1 + "," + y1 + ")");
            Console.WriteLine("(x2,y2) = (" + x2 + "," + y2 + ")");

            Console.WriteLine();
            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
            float SlopeM = (y2 - y1) / (x2 - x1);
            float interceptC = y1 / (SlopeM * x1);
            Console.WriteLine();
            Console.WriteLine("So the slope M is " + SlopeM + " and the intercept point C is " + x2);

            Console.ReadKey();
        }
    }
}

Lab2_Console_EquationOfALine

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