Algorithm
Sum of Consecutive Odd Numbers II
Adapted by Neilor Tonin, URI Brazil
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
Output
#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
Output
#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
Output