Answer to Question #158131 in C for Farjana Islam

Question #158131

Write a program to evalute the series.

1+3+5+7+........+n


1
Expert's answer
2021-01-25T03:40:04-0500
#include <stdio.h>
int main() {
    // we must calculte 1 + 3 + 5 + .... + n
    // n is odd
    int n, Sum = 0;
    printf("n = ");
    scanf("%d", &n);
    if (n % 2 == 1) {
        for (int i = 1; i <= n; i += 2) {
            Sum += i;
        }
    }
    else {
        printf("Enter only odd number.");
    }
    printf("The value of 1 + 3 + 5 + ... + %d = %d", n, Sum);
    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