Niloy007

Problem 2

May 25th, 2021
538
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. double power(double n) {
  4.     double res = 2;
  5.     for (int i = 1; i < n; i++) {
  6.         res *= 2;
  7.     }
  8.     return res;
  9. }
  10.  
  11. double series(double n) {
  12.     if (n == 0) {
  13.         return 1;
  14.     }
  15.     return (1.0 / power(n)) + series(n - 1);
  16. }
  17.  
  18. int main() {
  19.     double n;
  20.     scanf("%lf", &n);
  21.  
  22.     printf("Sum = %.3lf\n", series(n));
  23. }  
RAW Paste Data