Write a C program that computes the partial sum of the first k terms of the foll
ID: 3790063 • Letter: W
Question
Write a C program that computes the partial sum of the first k terms of the following series: sigma_n=1^k 1/n = 1 + 1/2 + 1/3 + 1/4 + ... + 1/k Your program should ask the user to enter a positive integer for k (> 1) and prints the calculation line by line. Assume that the user enters a correct input Your program should be able to print exactly the same as the following sample run. Calculate the sum of the following series: 1 + 1/2 + 1/3 + ... + 1/k Enter a positive number (> 1) for k: 28 1 + 1/2 = 1.500 1.500 + 1/3 = 1.833 1.833 + 1/4 = 2.083 2.083 + 1/5 = 2.283 2.283 + 1/6 = 2.450 2.450 + 1/7 = 2.593 2.593 + 1/8 = 2.718 2.718 + 1/9 = 2.829 2.829 + 1/10 = 2.929 2.929 + 1/11 = 3.020 3.020 + 1/12 = 3.103 3.103 + 1/13 = 3.180 3.180 + 1/14 = 3.252 3.252 + 1/15 = 3.318 3.318 + 1/16 = 3.381 3.381 + 1/17 = 3.440 3.440 + 1/18 = 3.495 3.495 + 1/19 = 3.548 3.548 + 1/20 = 3.598 3.598 + 1/21 = 3.645 3.645 + 1/22 = 3.691 3.691 + 1/23 = 3.734 3.734 + 1/24 = 3.77Explanation / Answer
#include <stdio.h>
int main()
{
int i, k;
double sum = 1;
printf("Calculate sum of the following series: 11 + 1/2 + 1/3 + .... + 1/k ");
printf("Enter a positive integer (> 1) for k: ");
scanf("%d", &k);//reading k value
for(i=2; i<=k; i++){//iterating each alue starting from 2 to k
sum = sum + 1/(double)i; //suming up each value
printf("1 + 1/%d = %.3lf ", i, sum); //displaying each sum in each iteration
}
return 0;
}
Output:
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Calculate sum of the following series:
11 + 1/2 + 1/3 + .... + 1/k
Enter a positive integer (> 1) for k: 28
1 + 1/2 = 1.500
1 + 1/3 = 1.833
1 + 1/4 = 2.083
1 + 1/5 = 2.283
1 + 1/6 = 2.450
1 + 1/7 = 2.593
1 + 1/8 = 2.718
1 + 1/9 = 2.829
1 + 1/10 = 2.929
1 + 1/11 = 3.020
1 + 1/12 = 3.103
1 + 1/13 = 3.180
1 + 1/14 = 3.252
1 + 1/15 = 3.318
1 + 1/16 = 3.381
1 + 1/17 = 3.440
1 + 1/18 = 3.495
1 + 1/19 = 3.548
1 + 1/20 = 3.598
1 + 1/21 = 3.645
1 + 1/22 = 3.691
1 + 1/23 = 3.734
1 + 1/24 = 3.776
1 + 1/25 = 3.816
1 + 1/26 = 3.854
1 + 1/27 = 3.891
1 + 1/28 = 3.927
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.