Program 6: Print the result by alternate sum and difference of a series

/* Program 6: Print the result by alternate sum and difference of a series */

       /* 1-2+3-4+5-……n */

 

#include<stdio.h>

int main()

{

        int i,sum=0,n,sign=1;

        do

        {

                 gotoxy(10,10);

                printf(“Enter any number (<=20) : “);

                scanf(“%d”,&n);

                if (n<1 || n>20)

                {

                        gotoxy(10,25);

                        printf(“Out of data range …. please try again”);

                 }

        }while(n<1 || n>20);

        gotoxy(10,10);

        printf(“The sum of the series is as follows….”);

        gotoxy(10,12);

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

        {

                sum=sum+i*sign;

                if (i%2==0)

                        printf(“%d+”,i);

                else

                        printf(“%d-“,i);

                sign=-sign;

        }

        printf(“\b=%d”,sum);

        return 0;

}