Algorithm


Problem Name: beecrowd | 1158

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1158

Sum of Consecutive Odd Numbers III

 

Adapted by Neilor Tonin, URI Brazil

Timelimit: 1

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 Y odd numbers from X 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 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 consecutive odd numbers from X.

 

 

 

Input Sample Output Sample

2
4 3
11 2

21
24

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <bits/stdc++.h>

using namespace std;

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

Input

x
+
cmd
2 4 3 11 2

Output

x
+
cmd
21 24

#2 Code Example with Java Programming

Code - Java Programming


import java.io.IOException;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) throws IOException{
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        while(n != 0){
            int x = input.nextInt();
            int y = input.nextInt();
            int ans = 0;
            while(y != 0){
                if (x % 2 != 0){
                    ans += x;
                    y -= 1;
                }
                x += 1;
            }
            System.out.println(ans);
            n -= 1;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
2 4 3 11 2

Output

x
+
cmd
21 24

#3 Code Example with Javascript Programming

Code - Javascript Programming


var input = require('fs').readFileSync('/dev/stdin', 'utf-8');
var sep = [' ', '\n'];
var line = input.split(new RegExp(sep.join('|'), 'g'));

var n = parseInt(line.shift());
while(n !== 0){
    var x = parseInt(line.shift());
    var y = parseInt(line.shift());
    var ans = 0;
    while(y !== 0){
        if (x % 2 !== 0){
            ans += x;
            y -= 1;
        }
        x += 1;
    }
    console.log(ans);
    n -= 1;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
2 4 3 11 2

Output

x
+
cmd
21 24

#4 Code Example with Python Programming

Code - Python Programming


n = int(input())
while(n != 0):
    x, y = map(int, input().split(" "))
    ans = 0
    while(y != 0):
        if (x % 2 != 0):
            ans += x
            y -= 1
        x += 1
    print(ans)
    n -= 1
Copy The Code & Try With Live Editor

Input

x
+
cmd
2 4 3 11 2

Output

x
+
cmd
21 24
Advertisements

Demonstration


Previous
#1157 Beecrowd Online Judge Solution 1157 Divisors I Solution in C++, Java, Js and Python
Next
#1159 Beecrowd Online Judge Solution 1159 Sum of Consecutive Even Numbers Solution in C++, Java, Js and Python