URI / BEE CROWD 1099 - Sum of Consecutive Odd Numbers II Solution in C,C++,Python

URI / BEE CROWD 1099 - Sum of Consecutive Odd Numbers II Solution in C,C++,Python | URI - BEECROWD - BEE 1099 Solution in C,C++,Python

                                                                                            beecrowd | 1099

Sum of Consecutive Odd Numbers II

Adapted by Neilor Tonin, URI  Brazil

                                                                                             Timelimit: 1


URI / BEE CROWD 1099 - Sum of Consecutive Odd Numbers II Solution in C,C++,Python | URI - BEECROWD - BEE 1099 Solution in C,C++,Python:

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

Input

The first line of 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 odd numbers between X and Y.

Input SampleOutput Sample

7

4 5

13 10

6 4

3 3

3 5

3 4

3 8

0

11

5

0

0

0

12

                                                          Demonstration:

This program takes user inputs . Then check the conditions for each test case contains two integer number x and y and compare those two numbers if x is greater than y , then swapping in ascending order and calculating the sum of all odds numbers by using condition in loop.

if(number%2 != 0) then it's an odd number. Then we add all those odd numbers.
Finally print the result.

N.B: Don't copy paste the code as same. Just try to understand it and try yourself.

  URI / BEE 1099 Solution in C :   

  URI / BEECROWD Online Judge 1099 Solve  in C :                                          

#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;
}

0 Response to URI / BEE CROWD 1099 - Sum of Consecutive Odd Numbers II Solution in C,C++,Python

Post a Comment