Website Summary Files Commits Branches @commit @diff @stats
path: root / Week05 / Exercise19.cpp
diff options
context:
space:
mode:
1 files changed, 30 insertions, 0 deletions
-rw-r--r--Week05/Exercise19.cpp30
diff --git a/Week05/Exercise19.cpp b/Week05/Exercise19.cpp
new file mode 100644
index 0000000..baad3c7
--- /dev/null
+++ b/Week05/Exercise19.cpp
@@ -0,0 +1,30 @@
+#include <iostream>
+
+int main()
+{
+ double x;
+ unsigned int n;
+ std::cin >> x >> n;
+
+ double sum = 0;
+
+ for (unsigned int k = 0; k <= n; k++) {
+ unsigned int l = 2 * k + 1;
+
+ double to_power = 1;
+ for (unsigned p = 1; p <= l; p++) {
+ to_power *= x;
+ }
+
+ unsigned int fac = 1;
+ for (unsigned i = 1; i <= l; i++) {
+ fac *= i;
+ }
+
+ sum += (k % 2 ? -1.0 : 1.0) * to_power / fac;
+ }
+
+ std::cout << sum << std::endl;
+
+ return 0;
+}