Friday, February 23, 2024

4. WAP to compute the sum of the first n terms of the following series S =1-2+3-4+5…………….

 #include <stdio.h>


int main()

{

    int n;

    int sum=0,i;

    printf("Enter the range of number:");

    scanf("%d",&n);


    for(i=1;i<=n;i++)

    {

        if(i%2==0)

            sum-=i;

        else

            sum+=i;

    }


    printf("The sum of the series = %d",sum);

    return 0;

}


No comments:

Post a Comment