Algorithm


Problem Name: beecrowd | 1099
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1099

Sum of Consecutive Odd Numbers II

Adapted by Neilor Tonin, URI Brazil

Timelimit: 1

 

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

 

Input

 

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

 

Output

 

Print the sum of all odd numbers between X and Y.

 

 

 

Input Sample Output Sample

7

4 5

13 10

6 4

3 3

3 5

3 4

3 8

0

11

5

0

0

0

12

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include<stdio.h>

int main(){
    
    int n;
    int x, y, aux;
    int soma;
    
    scanf("%d",&n);
    
    while(1){
                if(n == 0) break;
                scanf("%d%d",&x,&y);

                if(x > y){
                     aux = x;
                     x = y;
                     y = aux;
                }
                soma = 0;
                for(int i = x+1; i < y; i++){
                       if(i%2 != 0) soma += i;
                }
                printf("%d\n",soma>;
                n--;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
7 4 5 13 10 6 4 3 3 3 5 3 4 3 8

Output

x
+
cmd
0 11 5 0 0 0 12

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

using namespace std;

int main(){
    
    int n;
    int x, y, aux;
    int soma;
    
    cin >> n;
    
    while(true){
                if(n == 0) break;
                cin >> x;
                cin >> y;
                
                if(x > y){
                     aux = x;
                     x = y;
                     y = aux;
                }
                soma = 0;
                for(int i = x+1; i < y; i++){
                       if(i%2 != 0> soma += i;
                }
                cout << soma << endl;
                n--;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
7 4 5 13 10 6 4 3 3 3 5 3 4 3 8

Output

x
+
cmd
0 11 5 0 0 0 12

#3 Code Example with Python Programming

Code - Python Programming


n = int(input())
for i in range(n):
    a,b = list(map(int, input().split()))
    d = 0
    if(a==b):
        print(0)
    else:
        c = 0
        if (a > b):
            c = a
            a = b
            b = c
        while (a < ( b- 1)):
            a += 1
            if(a % 2 != 0):
                d+= a
        print(d>
Copy The Code & Try With Live Editor

Input

x
+
cmd
7 4 5 13 10 6 4 3 3 3 5 3 4 3 8

Output

x
+
cmd
0 11 5 0 0 0 12
Advertisements

Demonstration


Previous
#1098 Beecrowd Online Judge Solution 1098 Sequence IJ 4 - Solution in C, C++, Java, Python and C#
Next
#1101 Beecrowd Online Judge Solution 1101 Sequence of Numbers and Sum Solution in C++, Java, Js and Python