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 variablesn
andi
.float sum = 0.0;
: Initializes the variablesum
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 variablen
.for (i = 1; i <= n; ++i) { sum += 1.0 / i; }
: Iterates fromi=1
toi=n
and calculates the sum of the series by adding1.0/i
to thesum
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 themain()
function.