Beruflich Dokumente
Kultur Dokumente
IET, Katunayake
Assigning Values
// Example 2.3(Mathematical operations in C++)
#include <iostream>
using namespace std;
int main()
{
float a;
cout << "Enter any value for a:
";
cin >> a;
cout << "First value of a:
" << a << "\n";
a = a + 2;
// add two for first value
cout << "Second value of a:
" << a << "\n";
a = 2*a;
// multiply second value by 2
cout << "Third value of a:
" << a << "\n";
return 0;
// exit programme
return 0;
IET, Katunayake
IET, Katunayake
IET, Katunayake
IET, Katunayake
return 0;
IET, Katunayake
";
if (r == "simple")
{
cout << "ax + b = 0\n";
cout << "Enter a and b: ";
cin >> a >> b;
x = -b/a;
cout << "x = " << x << "\n";
}
else if (r == "quadratic")
{
cout << "ax2 + bx + c = 0\n";
cout << "Enter a, b and c: ";
cin >> a >> b >> c;
d = b*b - 4*a*c;
if (d >= 0)
{
d = sqrt (d);
x = (-b + d)/(2*a);
cout << "x = " << x << "\n";
x = (-b - d)/(2*a);
cout << "x = " << x << "\n";
}
if (d < 0)
{
d = sqrt (-d);
cout << "x = " << -b/(2*a) << " + i" << d/(2*a) << "\n";
cout << "x = " << -b/(2*a) << " - i" << d/(2*a) << "\n";
}
else
return 0;
// exit programme
10
11
IET, Katunayake