Assigning Values // Example 2.3(Mathematical operations in C++) 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"; // Example 2.3(Mathematical operations in C++) #include <math.h> 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 = log (a); // getting ln value cout << "ln of a: " << b << "\n"; " << b << "\n"; b = log10 (a); // getting log value cout << "log of a: " << b << "\n"; 5
Decisions and Loops // Example 3.1 (if loop) 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"; // output the result 6
// Example 3.2 (if loop) 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"; // output the result 7
// Example 3.3 (if loop & string) #include <string> using std::string; 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"; // output the result 8
// Example 3.4 (if, else if and else) 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"; // starting new line 9
// Example 3.5(solution for equation) #include <math.h> #include <string> using std::string; 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"; 10
11