Welcome to collectivesolver - Programming & Software Q&A with Examples. A website you can trust. All programs tested and work.

Contact: aviboots(AT)netvision.net.il


Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Survey Junkie is the #1 place online for your audience to earn rewards for their opinion.

100%
Managed VPS Hosting
Affordable. Secure. Your Own Virtual Private Hosting. Faster than AWS and Rackspace

Bonsai Boy of New York Bonsai Trees and Accessories
Liquid Web - Highest Level of Performance and Security with Managed Dedicated Servers

Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

21,340 questions

28,142 answers

573 users

How to find the roots of a quadratic equation in TypeScript

1 Answer

0 votes
function quadratic_equation_roots(a : number, b : number, c : number) {
    const discriminant = (b * b) - (4 * a * c);
 
    let root1 = 0, root2 = 0;
    if (discriminant > 0) {
        root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
        root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
        console.log(`root1 = ${root1}\nroot2 = ${root2}`);
    }
    else if (discriminant == 0) {
        root1 = -b / (2 * a);
        console.log(`root1 = root2 = ${root1}`);
    }
    else if (discriminant < 0) {
        const real = -b / (2 * a);
        const imaginary = Math.sqrt(-discriminant) / (2 * a);
        console.log(`root1 = ${real}+${imaginary}i\nroot2 = ${real}-${imaginary}i`);
    }
}
 
    
let a = 3, b = 5, c = -9;
 
quadratic_equation_roots(a, b, c);
console.log();
 
a = 3; b = 5; c = 7;
quadratic_equation_roots(a, b, c);
console.log();
 
a = 2; b = 4; c = 2;
quadratic_equation_roots(a, b, c);
 
 

 
 
/*
run:
 
"root1 = 1.0887604324451328
root2 = -2.7554270991117993" 

"root1 = -0.8333333333333334+1.2801909579781012i
root2 = -0.8333333333333334-1.2801909579781012i" 

"root1 = root2 = -1" 
 
*/

 




answered Jan 11 by avibootz
...