Question #47964

1+1/2+1/3+...+1/n
Write a program that compares the results of the
summation of the preceding series, computing from left to
right and from right to left with n = 50000

Expert's answer

#include <iostream>

#include <cstdio>

int main() {

int n = 50000;

double sum1 = 0;

double sum2 = 0;

for (int i = 1; i <= n; i++) {

sum1 += 1 / (double) i;

}

for (int i = n; i >= 1; i--) {

sum2 += 1 / (double) i;

}

printf("Sum from left to right: %f\n", sum1);

printf("Sum from right to left: %f", sum2);

}

#include <cstdio>

int main() {

int n = 50000;

double sum1 = 0;

double sum2 = 0;

for (int i = 1; i <= n; i++) {

sum1 += 1 / (double) i;

}

for (int i = n; i >= 1; i--) {

sum2 += 1 / (double) i;

}

printf("Sum from left to right: %f\n", sum1);

printf("Sum from right to left: %f", sum2);

}

## Comments

## Leave a comment