Click here to Skip to main content
11,799,282 members (69,588 online)
Click here to Skip to main content

Tagged as

A simple program to solve quadratic equations with

, 8 Nov 2010 CPOL 22.3K 3
Rate this:
Please Sign up or sign in to vote.
Simple and prints imaginary roots too!float a,b,c,x1,x2,d,dsq;printf("ax^2 + bx + c = 0");printf("\nEnter a,b,c separated by commas : \n");scanf("%f,%f,%f",&a,&b,&c);d = b*b-(4*a*c);if(d>=0){dsq=sqrt(d);x1 = (-b+dsq)/(2*a);x2 = (-b-(dsq))/(2*a);printf("\nRoot 1 : %f\nRoot 2...
Simple and prints imaginary roots too!
float a,b,c,x1,x2,d,dsq;
printf("ax^2 + bx + c = 0");
printf("\nEnter a,b,c separated by commas : \n");
d = b*b-(4*a*c);
x1 = (-b+dsq)/(2*a);
x2 = (-b-(dsq))/(2*a);
printf("\nRoot 1 : %f\nRoot 2 : %f",x1,x2);
d = ((4*a*c)-pow(b,2))/(2*a);
printf("\nRoot 1 : %f+%fi",((-b)/(2*a)),d);
printf("\nRoot 2 : %f-%fi",((-b)/(2*a)),d);}


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Anshul R
India India
No Biography provided

You may also be interested in...

Comments and Discussions

General1) It doesn't matter what the real world quadratic equations... Pin
Silic0re098-Nov-10 4:34
memberSilic0re098-Nov-10 4:34 
1) It doesn't matter what the real world quadratic equations say, if I run your program, I have the freedom to (stupidly or not), put a zero where asked for "a". The program should recognize this and ask for a different number, not create a divide by zero exception. One great axiom of programming is to ALWAYS ASSUME your input data is dirty, incorrect, and malicious. You need to protect your program from the user.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 8 Nov 2010
Article Copyright 2010 by Anshul R
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid