Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp exercises: Calculate root of Quadratic Equation


C# Sharp Conditional Statement: Exercise-11 with Solution

Write a C# Sharp program to calculate root of Quadratic Equation.

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise11  
{  
    public static void Main()
{
   int a,b,c;

   double d, x1,x2;
    Console.Write("\n\n");
    Console.Write("Calculate root of Quadratic Equation :\n");
    Console.Write("----------------------------------------");
    Console.Write("\n\n");
 
    Console.Write("Input the value of a : ");
    a = Convert.ToInt32(Console.ReadLine());
    Console.Write("Input the value of b : ");
    b = Convert.ToInt32(Console.ReadLine());
    Console.Write("Input the value of c : ");
    c = Convert.ToInt32(Console.ReadLine());

   d=b*b-4*a*c;
   if(d==0)
   {
     Console.Write("Both roots are equal.\n");
     x1=-b/(2.0*a);
     x2=x1;
     Console.Write("First  Root Root1= {0}\n",x1);
     Console.Write("Second Root Root2= {0}\n",x2);
   }
   else if(d>0)
	{
	   Console.Write("Both roots are real and diff-2\n");

	   x1=(-b+Math.Sqrt(d))/(2*a);
	   x2=(-b-Math.Sqrt(d))/(2*a);

	   Console.Write("First  Root Root1= {0}\n",x1);
	   Console.Write("Second Root root2= {0}\n",x2);
	}
	else
	    Console.Write("Root are imeainary;\nNo Solution. \n\n");
}
}

Sample Output:

Calculate root of Quadratic Equation :                                                                        
----------------------------------------                                                                                                        
Input the value of a : 1                                                                                      
Input the value of b : 2                                                                                      
Input the value of c : 1                                                                                      
Both roots are equal.                                                                                         
First  Root Root1= -1                                                                                         
Second Root Root2= -1 

Pictorial Presentation:

Conditional Statement: Calculate root of Quadratic Equation.

Flowchart:

Flowchart: Calculate root of Quadratic Equation.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C# Sharp program to find the eligibility of admission for a professional course based on the specific criteria.
Next: Write a C# Sharp program to read roll no, name and marks of three subjects and calculate the total, percentage and division.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.