Calculate the value of  this sum series [ S = 1 - 2 + 3 - 4 ... ± N ] 



Source code :-

#include<stdio.h>
int main()
{
    int i,n,s=0,j=1;
    printf("Enter the value of n: ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        s=s+(i*j);
        j=(j*(-1));
    }
    printf("Answer is => %d",s);
    return 1;
}

Input-Output :-

abhi@hp-15q-laptop:~$ gcc ss.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the value of n: 5
Answer is => 3
 

Post a Comment

Previous Post Next Post