Skip to main content

URI Online Judge Solution 1071 Sum of Consecutive Odd Numbers I



URI Online Judge Solution 1071   Sum of Consecutive Odd Numbers I using C Programming Language.
Read two integer values and Y. Print the sum of all odd values between them.

Input

The input file contain two integer values.

Output

The program must print an integer number. This number is the sum off all odd values between both input values that must fit in an integer number.
Sample InputSample Output
6
-5
5
15
12
13
12
12
0

Solution using C :

#include <stdio.h>
int main() {
    int x,y,i,s=0,t;
    scanf("%d%d",&x,&y);
    if(x>y)
    {
        t = x;
        x = y;
        y = t;
    }
    for(i=x+1; i<y; i++)
    {
        if(i%2 != 0)
            s += i;
    }
    printf("%d\n",s);
    return 0;
}

Comments