series

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

*/
#include<stdio.h>

main()
{
    int n, s=0,i,sign=1;
    printf("\nEnter value of n=");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        s=s+sign*(2*i-1);   
        sign=sign*(-1);
    }
    printf("\nThe sum of the series=%d",s);
}

No comments:

Post a Comment