CSharp - Write program to Solve the Linear Equation

Requirements

You will write a program to solve a linear equation, ax + b = 0.

For example, 2x + 6 = 0 is a linear equation, with the 2 being a and the 6 being b.

The user enters the equation to be solved in the form of the coefficients a and b.

The program then calculates and displays its solution.

Hint

To solve a linear equation:

If a is not zero, the obvious solution is -b/a.

Demo

using System;
class Program{//w  w  w .  ja  va2  s . c  om
    static void Main(string[] args)
    {
        // Inputs 
        Console.Write("Enter a: ");
        string inputA = Console.ReadLine();
        double a = Convert.ToDouble(inputA);

        Console.Write("Enter b: ");
        string inputB = Console.ReadLine();
        double b = Convert.ToDouble(inputB);

        // Solving the equation 
        if (a != 0)
        {
            // a is non-zero, the equation has "normal" solution 
            double solution = -b / a;
            Console.WriteLine("Solution is x=" + solution);
        }
        else
        {
            // a is zero, result depends on b 
            if (b == 0)
            {
                Console.WriteLine("The equation \"is solved\" by any x");
            }
            else
            {
                Console.WriteLine("The equation does not have a solution");
            }
        }
    }
}

Result