C++ program example-quadratic equation


Solving quadratic equation

This C++ example program is to calculate the root(s) of a quadratic equation: ax2+bx+c=0. The program firstly asks the user to input factors a, b, and c. The root(s) is calculated based on the following conditions:

-If a and b are zero, then there is no root.

-If a is zero, then there is one root, -c/b.

-If delta is less than 0, then there is no root.

-If all above conditions are not true, then there are two roots.

#include <cstdlib>

 #include <iostream>

 #include<cmath>

using namespace std;

int main(int argc, char *argv[]) {
float a, b,c; //declare local variables
float x,x1,x2,delta;

cout<<"Enter a, b, c:";
cin>>a>>b>>c;
if((a==0) && (b==0)) cout<<"No root"<else if(a==0) //one root
{
x=-c/b;
cout<<"One root:"<

}

else{
//calculate delta
delta=b*b-4*a*c;
if(delta<0) cout<<"No root"< else{ //two roots
x1=-b-sqrt(delta)/(2*a);
x2=-b+sqrt(delta)/(2*a);
cout<<"x1="< }
}


system("PAUSE");
return EXIT_SUCCESS; }




Comments

Stephanie  comment

 Stephanie

Write a program that reverses a string then displays it.
Function name: Reverse
This function returns a given string in reverse order. No cout statements are done in this function, only in int main().
Precondition: Receives one call by value string parameter.
Returns a string containing the reverse of its string parameter.
Use a while loop.
In the function int main()
The function asks the user for a word and calls Reverse with this value, receives the value back from the function and displays it.


2017-03-01
Stephanie  comment

 Stephanie

Write a program that reverses a string then displays it.
Function name: Reverse
This function returns a given string in reverse order. No cout statements are done in this function, only in int main().
Precondition: Receives one call by value string parameter.
Returns a string containing the reverse of its string parameter.
Use a while loop.
In the function int main()
The function asks the user for a word and calls Reverse with this value, receives the value back from the function and displays it.


2017-03-01
dylan  comment

 dylan

Good evening guys... im new in C++ looking for help. my question is ---

The system should contain (at least) the following options:
 Create a new purchase
 Search for a purchase
i. Delete a purchase
ii. Modify a purchase
 View purchases (most recent purchase must be viewed first)
i. Fulfill purchase
 View delivery list (express delivery must be viewed first)


any idea on how i need to do this ???


2017-02-18
dylan  comment

 dylan

Good evening guys... im new in C++ looking for help. my question is ---

The system should contain (at least) the following options:
 Create a new purchase
 Search for a purchase
i. Delete a purchase
ii. Modify a purchase
 View purchases (most recent purchase must be viewed first)
i. Fulfill purchase
 View delivery list (express delivery must be viewed first)


any idea on how i need to do this ???


2017-02-18
Leslie Jason comment

 Leslie Jason

#include <iostream>
using namespace std;

int main()
{
int a,b,c,d,e, avg;
cout<<"Enter your 5 values"<<endl;
cin>>a>>b>>c>>d>>e>>endl;
avg= (a+b+c+d+e)/5;
cout<<"The average of the values is<< avg<<endl;


2016-10-04
Kent comment

 Kent

Hi, Please help me with this..

"Write a program that asks the user to type 5 integers and writes the average of the 5 integers. "


2016-07-29



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.