59 lines
2.0 KiB
C++
59 lines
2.0 KiB
C++
//
|
|
// ***** Program Ch03_2.cpp ***********
|
|
// Example of arithmetic operations, increment and decrement
|
|
// operators
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
int main()
|
|
{
|
|
// declare variables for use in the program and assign them an initial
|
|
// value. These variables are used in various expressions to illustrate
|
|
// mathematical computations in C++
|
|
int num1 = 10, num2 = 20, num3 = 30;
|
|
float flt1 = 40, flt2 = 50.0, flt3 = 60.5;
|
|
int num;
|
|
float flt;
|
|
|
|
|
|
// Output the values of the variables used in the program. This information
|
|
// is then used to verify the results shown for the mathematical operations
|
|
|
|
cout << "num1 = " << num1 << " : num2 = " << num2 << " : num3 = " << num3 << endl;
|
|
cout << "flt1 = " << flt1 << " : flt2 = " << flt2 << " : flt3 = " << flt3 << endl;
|
|
cout << "=================================================================\n";
|
|
|
|
// Addition and subtraction
|
|
// Perform integer and floating point addition and subtraction and output
|
|
// the results.
|
|
cout << "Addition and subtraction\n\n";
|
|
num = num1 - num2 + num3 +5;
|
|
cout << "num1 - num2 + num3 +5 = " << num << endl;
|
|
flt = -flt1 + flt2 + flt3;
|
|
cout << "-flt1 + flt2 + flt3 = " << flt << endl;
|
|
cout << "=================================================================\n";
|
|
|
|
// Integer and Floating point multiplication examples
|
|
cout << "Multiplication\n\n";
|
|
cout << "num1*num2 = " << num1*num2 << endl;
|
|
cout << "flt1*flt3 = " << flt1*flt3 << endl;
|
|
cout << "=================================================================\n";
|
|
|
|
|
|
// Example of floating point division
|
|
cout << "floating point division\n";
|
|
cout << "flt1/flt2 = " << flt1/flt2 << endl;
|
|
cout << "16.0000/5.0000 = " << 16.0000/5.0000 << endl;
|
|
cout << "16.0000/4.0000 = " << 16.0000/4.0000 << endl;
|
|
|
|
// Example of integer division
|
|
cout << "\nInteger division: " << num1 << "/" << num2 << " = "
|
|
<< num1/num2 << endl;
|
|
cout << "integer division2, 19/3 = " << 19/3 << endl;
|
|
cout << "integer division3, 17/9 = " << 17/9 << endl;
|
|
cout << "=================================================================\n";
|
|
|
|
return 0;
|
|
}
|