Program 4: Print the sum of the odd numbers upto 1 & even numbers upto 2

/* Program 4: Print the sum of the odd numbers upto 1 & even numbers upto 2 */

/* n + (n-2) + (n-4) + …. + 1 or 2 depending on odd or even */

 

#include<stdio.h>

int main()

{

        int i,sum=0,n;

        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=n;i>=1;i=i-2)

        {

                sum=sum+i;

                printf(“%d+”,i);

        }

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

        return 0;

}