1099 Sum of Consecutive Odd Numbers II

SemiColon
By -
0
URI Online Judge | 1099

Sum of Consecutive Odd Numbers II

Read an integer that is the number of test cases. Each test case is a line containing two integer numbers and Y. Print the sum of all odd values between them, not including and Y.

Input

The first line of input is an integer that is the number of test cases that follow. Each test case is a line containing two integer and Y.

Output

Print the sum of all odd numbers between X and Y.
Input SampleOutput Sample
7
4 5
13 10
6 4
3 3
3 5
3 4
3 8
0
11
5
0
0
0
12




Solution:
#include <stdio.h>
int main()
{
    int N,X,Y,A,b,c=0;
    scanf("%d", &N);
    for(A=1;A<=N;A++)
    {
        scanf("%d%d",&X,&Y);
        if(X==Y)
        {
            c=0;
            printf("%d\n",c);
        }
        else if(X<Y)
        {
            for(b=X+1,c=0;b<Y;b++)
            {
                if(b%2==1||b%2==-1)
                    c+=b;
            }
            printf("%d\n",c);
        }
        else
        {
            for(b=Y+1,c=0;b<X;b++)
            {
                if(b%2==1||b%2==-1)
                    c+=b;
            }
            printf("%d\n",c);
        }
    }
    return 0;
}

Tags:

Post a Comment

0Comments

Post a Comment (0)