Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace QuadraticEquation
- {
- class QuadraticEquation
- {
- static void Main(string[] args)
- {
- try
- {
- Console.Write("Enter the equation this way - ax^2+bx+c=0: ");
- string equation = Console.ReadLine();
- int a = int.Parse(equation.Substring(0, 1));
- int b = int.Parse(equation.Substring(4, 2));
- int c = int.Parse(equation.Substring(7, 2));
- if (a != 0)
- {
- double discriminant = (b * b) - (4 * a * c);
- if (discriminant > 0)
- {
- double firstRoot = (-b + Math.Sqrt(discriminant)) / 2 * a;
- double secondRoot = (-b - Math.Sqrt(discriminant)) / 2 * a;
- Console.WriteLine("The roots of the equation are: {0} and {1}", Math.Round(firstRoot), Math.Round(secondRoot));
- }
- else if (discriminant == 0)
- {
- double root = -b / 2 * a;
- Console.WriteLine("The only root of the equation is {0}.", root);
- }
- else
- {
- Console.WriteLine("The equation has no real roots.");
- }
- }
- else
- {
- double singleRoot = -c / b;
- Console.WriteLine("The equation has a single root - {0}.", Math.Round(singleRoot, 2));
- }
- }
- catch (FormatException)
- {
- Console.WriteLine("You have entered some invalid data. Please try again.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement