Answer to Question #310475 in C for Samman

Question #310475

Write a program to find sum of following series for a given value of ‘n’:



A) 1 + 1/2 - 1/3 + 1/4 - 1/5 + 1/6 +……+ 1/n

1
Expert's answer
2022-03-12T18:05:08-0500
#include <stdio.h>


int main() {
    int n, i;
    double s;


    printf("Enter a number: ");
    scanf("%d", &n);


    s = 1.0;
    for(i=2; i<=n; i++) {
        if (i%2 == 0) {
            s += 1.0 / i;
        }
        else {
            s -= 1.0 /i;
        }
    }
    printf("The sum is equal %lf", s);


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS