latex sum

#include <stdio.h>

int main() {
    int n, i;
    float sum = 0.0;

    printf("Enter the number of terms: ");
    scanf("%d", &n);

    for (i = 1; i <= n; ++i) {
        sum += 1.0 / i;
    }

    printf("The sum of the series is: %.2f", sum);

    return 0;
}
\sum_{i=1}^{n} \frac{1}{i}

Explanation:

  • #include <stdio.h>: Includes the standard input/output library for input/output functions.
  • int main(): The main function where the execution of the program begins.
  • int n, i;: Declares integer variables n and i.
  • float sum = 0.0;: Initializes the variable sum as a floating-point number with a value of 0.0.
  • printf("Enter the number of terms: ");: Displays a message asking the user to enter the number of terms in the series.
  • scanf("%d", &n);: Reads the user input for the number of terms and stores it in the variable n.
  • for (i = 1; i <= n; ++i) { sum += 1.0 / i; }: Iterates from i=1 to i=n and calculates the sum of the series by adding 1.0/i to the sum in each iteration.
  • printf("The sum of the series is: %.2f", sum);: Prints the calculated sum of the series with two decimal places.
  • return 0;: Indicates successful execution and termination of the main() function.