

cpp
#include <iostream>
using namespace std;
double power(double x, int n) {
double val = 1.0;
while (n--) {
val *= x;
}
return val;
}
int main() {
cout << "5 to the power 2 is " << power(5, 2) << endl;
return 0;
}

cpp
#include <iostream>
using namespace std;
double power(double x, int n);
int main() {
int value = 0;
cout << "Enter an 8 bit binary number ";
for (int i = 7; i >= 0; i--) {
char ch;
cin >> ch;
if (ch == '1') {
value += static_cast<int>(power(2, i));
}
}
cout << "Decimal value is " << value << endl;
return 0;
}
double power(double x, int n) {
double val = 1.0;
while (n--) {
val *= x;
}
return val;
}

cpp
#include <iostream>
using namespace std;
double arctan(double x) {
double sqr = x * x;
double e = x;
double r = 0;
int i = 1;
while (e / i > 1e-15) {
double f = e / i;
r = (i % 4 == 1) ? r + f : r - f;
e = e * sqr;
i += 2;
}
return r;
}
int main() {
double a = 16.0 * arctan(1 / 5.0);
double b = 4.0 * arctan(1 / 239.0);
cout << "PI=" << a - b << endl;
return 0;
}

cpp
#include <iostream>
using namespace std;
bool symm(unsigned n) {
unsigned i = n;
unsigned m = 0;
while (i > 0) {
m = m * 10 + i % 10;
i /= 10;
}
return m == n;
}
int main() {
for (unsigned m = 11; m < 1000; m++) {
if (symm(m) && symm(m * m) && symm(m * m * m)) {
cout << "m = " << m;
cout << "m * m = " << m * m;
cout << "m * m * m = " << m * m * m << endl;
}
}
return 0;
}

cpp
#include <iostream>
#include <cmath>
using namespace std;
const double TINY_VALUE = 1e-10;
double tsin(double x) {
double g = 0;
double t = x;
int n = 1;
do {
g += t;
n++;
t = -t * x * x / (2 * n - 1) / (2 * n - 2);
} while (fabs(t) >= TINY_VALUE);
return g;
}
int main() {
double k, r, s;
cout << "r=";
cin >> r;
cout << "s=";
cin >> s;
if (r * r <= s * s) {
k = sqrt(tsin(r) * tsin(r) + tsin(s) * tsin(s));
}
else {
k = tsin(r * s) / 2;
}
cout << k << endl;
return 0;
}


cpp
#include <iostream>
#include <cstdlib>
using namespace std;
enum GameStatus{WIN,LOSE,PLAYING};
int rollDice() {
int die1 = 1 + rand() % 6;
int die2 = 1 + rand() % 6;
int sum = die1 + die2;
cout << "player rolled" << die1 << " + " << die2 << " = " << sum << endl;
return sum;
}
int main() {
int sum, myPoint;
GameStatus status;
unsigned seed;
cout << "Please enter an unsigned integer:";
cin >> seed;
srand(seed);
sum = rollDice();
switch (sum) {
case 7:
case 11:
status = WIN;
break;
case 2:
case 3:
case 12:
status = LOSE;
break;
default:
status = PLAYING;
myPoint = sum;
cout << "point is " << myPoint << endl;
break;
}
while (status == PLAYING) {
sum = rollDice();
if (sum == myPoint) {
status = WIN;
}
else if (sum == 7) {
status == LOSE;
}
}
if (status == WIN) {
cout << "player win" << endl;
}
else {
cout << "player lose" << endl;
}
return 0;
}
cpp
#include <iostream>
using namespace std;
int fun2(int m) {
return m * m;
}
int fun1(int x, int y) {
return fun2(x) + fun2(y);
}
int main() {
int a, b;
cout << "Please enter two integers (a and b)";
cin >> a >> b;
cout << "The sum of sqare of a and b:"
<< fun1(a, b) << endl;
return 0;
}



cpp
#include <iostream>
using namespace std;
unsigned fac(int n) {
unsigned f;
if (n == 0) {
f = 1;
}
else {
f = fac(n - 1) * n;
}
return f;
}
int main() {
unsigned n;
cout << "Enter a positive integer:";
cin >> n;
unsigned y = fac(n);
cout << n << "!=" << fac(n) << endl;
return 0;
}

cpp
#include <iostream>
using namespace std;
int comm(int n, int k) {
if (k > n) {
return 0;
}
else if (n == k || k == 0) {
return 1;
}
else {
return comm(n - 1, k - 1) + comm(n - 1, k);
}
}
int main() {
int n, k;
cout << "Please enter two integers n and k";
cin >> n >> k;
cout << "C(n,k) = " << comm(n, k) << endl;
return 0;
}


cpp
#include <iostream>
using namespace std;
void move(char src, char dest) {
cout << src << "-->" << dest << endl;
}
int x = 0;
void hanoi(int n, char src, char medium, char dest) {
x++;
if (n == 1) {
move(src, dest);
}
else {
hanoi(n - 1, src, dest, medium);
move(src, dest);
hanoi(n - 1, medium, src, dest);
}
}
int main() {
int m;
cout << "Enter the number of disks:";
cin >> m;
cout << "the steps of moving " << m << "disks:" << endl;
hanoi(m, 'A', 'B', 'C');
cout << x;
return 0;
}

cpp
#include <iostream>
using namespace std;
void swap(int a, int b) {
int t = a;
a = b;
b = t;
}
int main() {
int x = 5, y = 6;
cout << "x = " << x << " y = " << y << endl;
swap(x, y);
cout << "x = " << x << " y = " << y << endl;
return 0;
}
cpp
#include <iostream>
using namespace std;
void swap(int& a, int& b) {
int t = a;
a = b;
b = t;
}
int main() {
int x = 5, y = 10;
cout << "x = " << x << " y = " << y << endl;
swap(x, y);
cout << "x = " << x << " y = " << y << endl;
return 0;
}



cpp
#include <iostream>
using namespace std;
const double PI = 3.14159265358979;
inline double calArea(double radius) {
return PI * radius * radius;
}
int main() {
double r = 3.0;
double area = calArea(r);
cout << area << endl;
return 0;
}




cpp
#include <iostream>
#include <iomanip>
using namespace std;
int getVolume(int length, int width = 2, int height = 3);
int main() {
const int X = 10, Y = 12, Z = 15;
cout << "Some box data is";
cout << getVolume(X, Y, Z) << endl;
cout << "Some box data is";
cout << getVolume(X, Y) << endl;
cout << "Some box data is";
cout << getVolume(X) << endl;
return 0;
}
int getVolume(int length, int width, int height) {
cout << setw(5) << length << setw(5) << width << setw(5) << height << "\t";
return length * width * height;
}



cpp
#include <iostream>
using namespace std;
int sumOfSquare(int a, int b) {
return a * a + b * b;
}
double sumOfSquare(double a, double b) {
return a * a + b * b;
}
int main() {
int m, n;
cout << "Enter two integer:";
cin >> m >> n;
cout << "Their sum of square:" << sumOfSquare(m, n) << endl;
double x, y;
cout << "Enter two real number";
cin >> x >> y;
cout << "Their sum of square:" << sumOfSquare(x, y) << endl;
return 0;
}


cpp
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.14159265358979;
int main() {
double angle;
cout << "Please enter an angle";
cin >> angle;
double radian = angle * PI / 180;
cout << "sin(" << angle << ")=" << sin(radian) << endl;
cout << "cos(" << angle<<")=" << cos(radian) << endl;
cout << "tan(" << angle<<")=" << tan(radian) << endl;
return 0;
}

