Tuesday, May 24, 2016

NIELIT M3-R4: Q.No. 6(a) July 2014

M3-R4: July 2014 (O-Level)

Q. 6(a):
Write a program to determine the sum of the following series:
S = 1 – 3 + 5 – 7 + …n
Read the value of n from the user.

Ans:
Program to determine the sum of the above series:

#include <stdio.h>
#include <conio.h>
#include <math.h>

void main(){
    int sum=0, n, i, term;
    printf("\nEnter number of terms (n):");
    scanf("%d", &n);
    for(i=0; i<n; i++){
        term = (2*i+1) * pow(-1,i);
        sum = sum + term;
    }
    printf("\nThe sum of the series is %d", sum);
    getch();
}

No comments:

Post a Comment