Calculate the product of three integers
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int x;
int y;
int z;
int result;
cout << "Enter three integers: ";
cin >> x >> y >> z; // read three integers from user
result = x * y * z;
cout << "The product is " << result << endl;
return 0;
}
Enter three integers: 1 2
3
The product is 6
Calculate the value of product and quotient
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int x = 5;
int product = 5;
int quotient = 5;
product *= x++;
cout << product << endl;
cout << x << endl << endl;
x = 5;
quotient /= ++x;
cout << quotient << endl;
cout << x << endl;
return 0;
}
25
6
0
6
Comparing integers using if statements, relational operators and equality operators.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int number1;
int number2;
cout << "Enter two integers to compare: ";
cin >> number1 >> number2;
if ( number1 == number2 )
cout << number1 << " == " << number2 << endl;
if ( number1 != number2 )
cout << number1 << " != " << number2 << endl;
if ( number1 < number2 )
cout << number1 << " < " << number2 << endl;
if ( number1 > number2 )
cout << number1 << " > " << number2 << endl;
if ( number1 <= number2 )
cout << number1 << " <= " << number2 << endl;
if ( number1 >= number2 )
cout << number1 << " >= " << number2 << endl;
return 0;
}
Enter two integers to compare: 4
3
4 != 3
4 > 3
4 >= 3
constant integer value
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
const double RATE_MIN = 5;
const double RATE_MAX = 10;
const double RATE_INCR = 0.5;
const int YEAR_MIN = 5;
const int YEAR_MAX = 30;
const int YEAR_INCR = 5;
/* print table header */
int year;
cout << " Rate ";
for (year = YEAR_MIN; year <= YEAR_MAX; year = year + YEAR_INCR)
{
cout << setw(2) << year << " years ";
}
cout << "\n";
cout << fixed << setprecision(2);
double initial_balance = 10000;
double rate;
for (rate = RATE_MIN; rate <= RATE_MAX; rate = rate + RATE_INCR)
{
/* print table row */
cout << setw(5) << rate;
for (int year = YEAR_MIN; year <= YEAR_MAX;
year = year + YEAR_INCR)
{
double balance =
initial_balance * pow(1 + rate / 100, year);
cout << setw(10) << balance;
}
cout << "\n";
}
return 0;
}
Convert gallons to liters
#include <iostream>
using namespace std;
int main()
{
int gallons, liters;
cout << "Enter number of gallons: ";
cin >> gallons; // this inputs from the user
liters = gallons * 4; // convert to liters
cout << "Liters: " << liters;
return 0;
}
Enter number of gallons: 12
Liters: 48
Define and assign int variables
#include <iostream>
int main()
{
int a=0, b=0, x=0, y=35;
std::cout << "a: " << a << " b: " << b;
std::cout << " x: " << x << " y: " << y << std::endl;
a = 9;
b = 7;
y = x = a+b;
std::cout << "a: " << a << " b: " << b;
std::cout << " x: " << x << " y: " << y << std::endl;
return 0;
}
a: 0 b: 0 x: 0 y: 35
a: 9 b: 7 x: 16 y: 16
Define an int variable
#include <iostream>
using namespace std;
int main()
{
int length; // this declares a variable
length = 7; // this assigns 7 to length
cout << "The length is ";
cout << length; // This displays 7
return 0;
}
The length is 7"
Do bit operation on integers: AND, OR, XOR
#include <iostream>
using namespace std;
int main()
{
int num1, num2,iand,ior,ixor;
num1 = 1;
num2 = 2;
iand = num1 & num2;
ior = num1 | num2;
ixor = num1 ^ num2;
cout << num1 << " AND " << num2 << " is " << iand << endl;
cout << num1 << " OR " << num2 << " is " << ior << endl;
cout << num1 << " XOR " << num2 << " is " << ixor << endl;
return 0;
}
1 AND 2 is 0
1 OR 2 is 3
1 XOR 2 is 3
Do calculation in cout
#include <iostream>
using namespace std;
int main()
{
int length;
int width;
length = 7;
width = 5;
cout << "The area is ";
cout << length * width;
return 0;
}
The area is 35"
lists cubes from 1 to 10: int type variable calculation
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int numb;
for(numb=1; numb<=10; numb++){
cout << setw(4) << numb; //display 1st column
int cube = numb*numb*numb; //calculate cube
cout << setw(6) << cube << endl; //display 2nd column
}
return 0;
}
simple FOR loop controlled by int type variable
#include <iostream>
using namespace std;
int main(){
int j;
for(j=0; j<15; j++)
cout << j * j << " ";
cout << endl;
return 0;
}
Use arithmetic operator to get the product of two integers
#include <iostream>
using namespace std;
int main()
{
int length;
int width;
int area;
length = 7;
width = 5;
area = length * width; // compute area
cout << "The area is ";
cout << area;
return 0;
}
The area is 35"