# Rust program to find the roots of a quadratic equation

Given the values of a, b and c, find the roots of a quadratic equation using Rust program.
Submitted by Nidhi, on September 29, 2021

Problem Solution:

Here, we will read the value of a, b, and c, and calculate the roots of a quadratic equation.

Program/Source Code:

The source code to the roots of a quadratic equation is given below. The given program is compiled and executed successfully.

```// Rust program to roots of a quadratic equation

use std::io;

fn main() {
let mut a:f32 = 0.0;
let mut b:f32 = 0.0;
let mut c:f32 = 0.0;

let mut rootA:f32 = 0.0;
let mut rootB:f32 = 0.0;

let mut realp:f32 = 0.0;
let mut imagp:f32 = 0.0;
let mut disc:f32  = 0.0;

let mut input1 = String::new();
let mut input2 = String::new();
let mut input3 = String::new();

println!("Enter A: ");
a = input1.trim().parse().expect("Not a valid number");

println!("Enter B: ");
b = input2.trim().parse().expect("Not a valid number");

println!("Enter C: ");
c = input3.trim().parse().expect("Not a valid number");

if a == 0.0 || b == 0.0 || c == 0.0
{
println!("Error: Unable to determine roots");
return;
}
else
{
disc = b * b - 4.0 * a * c;
if disc < 0.0
{
println!("Imaginary Roots");
realp = -b / (2.0 * a);
disc = disc.abs();
imagp = disc.sqrt() / (2.0 * a);
println!("Root1 = {}  +i {}", realp, imagp);
println!("Root2 = {}  -i {}", realp, imagp);
}
else if disc > 0.0
{
println!("Roots are real and distinct");
rootA = (-b + disc.sqrt()) / (2.0 * a);
rootB = (-b - disc.sqrt()) / (2.0 * a);
println!("Root1 = {}  ", rootA);
println!("Root2 = {}  ", rootB);
}
else if disc == 0.0
{
println!("Roots are real and equal");
rootA = -b / (2.0 * a);
rootB = rootA;
println!("Root1 = {}", rootA);
println!("Root2 = {}", rootB);
}
}
}
```

Output:

```Enter A:
20
Enter B:
30
Enter C:
40
Imaginary Roots
Root1 = -0.75  +i 1.1989579
Root2 = -0.75  -i 1.1989579
```

Explanation:

Here, we read the value of a, b, c from the user. Then we calculated the roots of a quadratic equation.