Skip to main content

URI Problem 1158 Solution Sum of Consecutive Odd Numbers III - URI Online Judge Solution



URI Online Judge Solution 1158 Sum of Consecutive Odd Numbers III using C, Python Programming Language.

Read an integer N that is the number of test cases that follows. Each test case contains two integers X and Y. Print one output line for each test case that is the sum of odd numbers from including it if is the case. For example:
for the input 4 5, the output must be 45, that is: 5 + 7 + 9 + 11 + 13
for the input 7 4, the output must be 40, that is: 7 + 9 + 11 + 13

Input

The first line of the 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 consecutive odd numbers from X.

Input SampleOutput Sample

2
4 3
11 2

21
24

URI 1158 Solution in C:

#include <stdio.h>

 int main() {

    int T,x,y,s;

    scanf("%d",&T);

    while(T--)

    {

        s=0;

        scanf("%d%d",&x,&y);

        while(y)

        {

            if(x%2!=0)

            {

                s += x;

                x++;

                y--;

            }

            else

            {

                x++;

            }

        }

        printf("%d\n",s);

    }

  return 0;

}

URI 1158 Solution in Python:

Comments