Calcluate Exponents of a double value
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
double radius, area;
cout << "Enter radius of circle: ";
cin >> radius;
area = 3.14159 * pow(radius, 2);
cout << "The area is " << area << endl;
return 0;
}
Calculate the future value
#include <iostream>
#include <cmath>
using namespace std;
double future_value(double p)
{
double b = 1000 * pow(1 + p / 100, 10);
return b;
}
int main()
{
cout << "Please enter the interest rate in percent: ";
double rate;
cin >> rate;
double balance = future_value(rate);
cout << "After 10 years, the balance is "
<< balance << "\n";
return 0;
}
Calculation by double variables
#include <iostream>
using namespace std;
int main() {
double distance;
double lightspeed;
double delay;
double delay_in_min;
distance = 34000000.0; // 34,000,000 miles
lightspeed = 186000.0; // 186,000 per second
delay = distance / lightspeed;
cout << "Time delay when talking to Mars: " <<
delay << " seconds.\n";
delay_in_min = delay / 60.0;
cout << "This is " << delay_in_min << " minutes.";
return 0;
}
Time delay when talking to Mars: 182.796 seconds.
This is 3.04659 minutes."
Check the value of a double read from keyboard
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
double area;
cout << "Please enter the area of a square: ";
cin >> area;
if (area >= 0)
cout << "The side length is " << sqrt(area) << "\n";
else
cout << "Error: Negative area.\n";
return 0;
}
Compare double base on precision
#include <iostream>
#include <cmath>
using namespace std;
bool doubleEquals(double left, double right, double epsilon) {
return (fabs(left - right) < epsilon);
}
bool doubleLess(double left, double right, double epsilon, bool orequal = false) {
if (fabs(left - right) < epsilon) {
return (orequal);
}
return (left < right);
}
bool doubleGreater(double left, double right, double epsilon, bool orequal = false) {
if (fabs(left - right) < epsilon) {
return (orequal);
}
return (left > right);
}
int main( ) {
double first = 0.33333333;
double second = 1.0 / 3.0;
cout << first << endl;
cout << second << endl;
cout << boolalpha << (first == second) << endl;
cout << doubleEquals(first, second, .0001) << endl;
cout << doubleLess(first, second, .0001) << endl;
cout << doubleGreater(first, second, .0001) << endl;
cout << doubleLess(first, second, .0001, true) << endl;
cout << doubleGreater(first, second, .0001, true) << endl;
}
0.333333
0.333333
false
true
false
false
true
true
Compare double in if statement
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter a magnitude on the Richter scale: ";
double richter;
cin >> richter;
if (richter >= 8.0)
cout << "Most structures fall\n";
else if (richter >= 7.0)
cout << "Many buildings destroyed\n";
else if (richter >= 6.0)
cout << "Many buildings considerably damaged, "
<< "some collapse\n";
else if (richter >= 4.5)
cout << "Damage to poorly constructed buildings\n";
else if (richter >= 3.5)
cout << "Felt by many people, no destruction\n";
else if (richter >= 0)
cout << "Generally not felt by people\n";
else
cout << "Negative numbers are not valid\n";
return 0;
}
Compute the regular payments for a loan.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double Principal = 11.11;
double IntRate = 0.075;
double PayPerYear = 12.0;
double NumYears = 25;
double Payment;
double numer, denom;
double b, e;
numer = IntRate * Principal / PayPerYear;
e = -(PayPerYear * NumYears);
b = (IntRate / PayPerYear) + 1;
denom = 1 - pow(b, e);
Payment = numer / denom;
cout << "Payment is " << Payment;
return 0;
}
Payment is 0.0821019"
Convert feet to meters using doubles
#include <iostream>
using namespace std;
int main() {
double f; // holds the length in feet
double m; // holds the conversion to meters
cout << "Enter the length in feet: ";
cin >> f; // read the number of feet
m = f / 3.28; // convert to meters
cout << f << " feet is " << m << " meters.";
return 0;
}
Enter the length in feet: 12
12 feet is 3.65854 meters."
Differences between int and double during "divide"
#include <iostream>
using namespace std;
int main() {
int ivar;
double dvar;
ivar = 100;
dvar = 100.0;
cout << "Original value of ivar: " << ivar << "\n";
cout << "Original value of dvar: " << dvar << "\n";
cout << "\n";
ivar = ivar / 3;
dvar = dvar / 3.0;
cout << "ivar after division: " << ivar << "\n";
cout << "dvar after division: " << dvar << "\n";
return 0;
}
Original value of ivar: 100
Original value of dvar: 100
ivar after division: 33
dvar after division: 33.3333
Do while loop: double value and fabs()
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Please enter a number: ";
double a;
cin >> a;
const double EPSILON = 1E-14;
double xnew = a;
double xold;
do
{
xold = xnew;
xnew = (xold + a / xold) / 2;
}
while (fabs(xnew - xold) > EPSILON);
cout << "The square root is " << xnew << "\n";
return 0;
}
how many rolls of wallpaper are required for a room with double type variables
#include <iostream>
using namespace std;
int main()
{
double height = 2.2, width = 3.3, length = 4.4;
double perimeter = 0.0;
double rollwidth = 21.0;
double rolllength = 12.0*33.0;
int strips_per_roll = 0;
int strips_reqd = 0;
int nrolls = 0;
strips_per_roll = rolllength / height;
perimeter = 2.0*(length + width);
strips_reqd = perimeter / rollwidth;
nrolls = strips_reqd / strips_per_roll;
cout << nrolls;
return 0;
}
Show decimal point field when displaying double and set filler
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char** argv)
{
double dbl = 1.452;
double dbl2 = 5;
cout << "This should be 5: " << setw(2) << noshowpoint << dbl2 << endl;
cout << "This should be @@1.452: " << setw(7) << setfill("@") << dbl << endl;
}
Show square roots of 1 to 99
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num;
double sq_root;
for(num=1; num < 100; num++) {
sq_root = sqrt((double) num);
cout << num << " " << sq_root << "\n";
}
return 0;
}
1 1
2 1.41421
3 1.73205
4 2
5 2.23607
6 2.44949
7 2.64575
8 2.82843
9 3
10 3.16228
11 3.31662
12 3.4641
13 3.60555
14 3.74166
15 3.87298
16 4
17 4.12311
18 4.24264
19 4.3589
20 4.47214
21 4.58258
22 4.69042
23 4.79583
24 4.89898
25 5
26 5.09902
27 5.19615
28 5.2915
29 5.38516
30 5.47723
31 5.56776
32 5.65685
33 5.74456
34 5.83095
35 5.91608
36 6
37 6.08276
38 6.16441
39 6.245
40 6.32456
41 6.40312
42 6.48074
43 6.55744
44 6.63325
45 6.7082
46 6.78233
47 6.85565
48 6.9282
49 7
50 7.07107
51 7.14143
52 7.2111
53 7.28011
54 7.34847
55 7.4162
56 7.48331
57 7.54983
58 7.61577
59 7.68115
60 7.74597
61 7.81025
62 7.87401
63 7.93725
64 8
65 8.06226
66 8.12404
67 8.18535
68 8.24621
69 8.30662
70 8.3666
71 8.42615
72 8.48528
73 8.544
74 8.60233
75 8.66025
76 8.7178
77 8.77496
78 8.83176
79 8.88819
80 8.94427
81 9
82 9.05539
83 9.11043
84 9.16515
85 9.21954
86 9.27362
87 9.32738
88 9.38083
89 9.43398
90 9.48683
91 9.53939
92 9.59166
93 9.64365
94 9.69536
95 9.74679
96 9.79796
97 9.84886
98 9.89949
99 9.94987
Use double value as the while loop counter
#include <iostream>
using namespace std;
int main()
{
double rate = 5;
double initial_balance = 10000;
double balance = initial_balance;
int year = 0;
while (balance < 2 * initial_balance){
balance = balance * (1 + rate / 100);
year++;
}
cout << "The investment doubled after " << year << " years.\n";
return 0;
}
Use the Pythagorean theorem to find find the length of the hypotenuse
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x, y, z;
x = 5;
y = 4;
z = sqrt(x*x + y*y);
cout << "Hypotenuse is " << z;
return 0;
}
Hypotenuse is 6.40312"