You are on page 1of 7

Object Oriented Programming Using C++

Mathematics & Computing

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

// Example 2.3(Mathematical operations in C++)


#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float a , b;
cout << "Enter any value for a:
";
cin >> a;
b = pow (a,7); // sevent power of a
cout << "seventh power of a: " << b << "\n";
b = sqrt (a);
// getting square root
cout << "Square root of a:
" << b << "\n";
b = log (a); // getting ln value
cout << "ln of a: " << b << "\n";
b = log10 (a); // getting log value
cout << "log of a:
" << b << "\n";
}

return 0;

Object Oriented Programming Using C++

Mathematics & Computing

IET, Katunayake

Decisions and Loops


// Example 3.1 (if loop)
#include <iostream>
using namespace std;
int main()
{
int a;
// declaring integer
float b, c, d;
// declaring values
cout << "Enter 1 for addition or enter 2 for multiplication ";
cin >> a;
cout << "enter two integer values: ";// output the requirement
cin >> b >> c;
// asign the values
if (a == 1)
{
d = b + c;
}
if (a == 2)
{
d = b*c;
}

cout << d << "\n";


return 0;

// output the result


// exit programme

Object Oriented Programming Using C++

Mathematics & Computing

IET, Katunayake

// Example 3.2 (if loop)


#include <iostream>
using namespace std;
int main()
{
char r;
// declaring character
float b, c, d;
// declaring values
cout << "Enter a for addition or enter m for multiplication ";
cin >> r;
cout << "enter two integer values: ";
// output the requirement
cin >> b >> c;
// asign the values
if (r == 'a')
{
d = b + c;
}
if (r == 'm')
{
d = b*c;
}

cout << d << "\n";


return 0;

// output the result


// exit programme

Object Oriented Programming Using C++

Mathematics & Computing

IET, Katunayake

// Example 3.3 (if loop & string)


#include <iostream>
#include <string>
using namespace std;
using std::string;
int main()
{
string r;
// declaring string
float b, c, d;
// declaring values
cout << "Enter addition or multiplication:
";
cin >> r;
cout << "enter two values: ";
// output the requirement
cin >> b >> c;
// asign the values
if (r == "addition")
{
d = b + c;
}
if (r == "multiplication")
{
d = b*c;
}
cout <<" " << d << "\n";
return 0;

// output the result


// exit programme

Object Oriented Programming Using C++

Mathematics & Computing

IET, Katunayake

// Example 3.4 (if, else if and else)


#include <iostream>
using namespace std;
int main()
{
char c;
// declaring character
cout << "Enter mathematical operation:
";
cin >> c;
// asign the character
if (c == '+')
{
cout << "Symbol of addition";
}
else if (c == '-')
{
cout << "Symbol of subtract";
}
else if (c == '*')
{
cout << "Symbol of multiplication";
}
else if (c == '/')
{
cout << "Symbol of division";
}
else

cout << "Not a mathematical symbol";

cout << "\n";


}

return 0;

// starting new line


// exit programme

Object Oriented Programming Using C++

Mathematics & Computing

IET, Katunayake

// Example 3.5(solution for equation)


#include <iostream>
#include <math.h>
#include <string>
using namespace std;
using std::string;
int main()
{
float a, b, c, d, x;
string r;
cout << "Enter type of equation (simple or quadratic):
cin >> r;

";

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

cout << "error input\n";

return 0;

// exit programme

10

Object Oriented Programming Using C++

Mathematics & Computing

11

IET, Katunayake

You might also like