Monday, May 30, 2016

URI Online Judge Solution 1099 || Sum of Consecutive Odd Numbers II

URI Online Judge | 1099

Sum of Consecutive Odd Numbers II

Adapted by Neilor Tonin, URI  Brazil
Timelimit: 1
Read an integer that is the number of test cases. Each test case is a line containing two integer numbers Xand 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


URI Online Judge Solution 1099 || Sum of Consecutive Odd Numbers II in JAVA

import java.io.IOException;
import java.util.Scanner;
public class Main {
 
    public static void main(String[] args) throws IOException {
 
        int N;
        int interval_start,interval_end;
        Scanner input =new Scanner(System.in);
         
        N =input.nextInt();
        for (int i = 1; i <= N; i++) {
            int total_odd = 0;
            interval_start = input.nextInt();
            interval_end = input.nextInt();
            if (interval_start > interval_end) {
                for (int j = interval_start-1; j > interval_end; j--) {
                    if (j % 2 != 0) {
 
                        total_odd+=j;
                    }
                }
            }else if(interval_start < interval_end){
                for (int j = interval_start + 1; j < interval_end; j++) {
                    if (j % 2 != 0) {
 
                        total_odd+=j;
                    }
                    }
                 
                }else {
                    total_odd = 0;
                }
 
            System.out.print(total_odd+"\n");
        }
         
         
 
    }
}



Download code from dropbox
Main Question link

1 comment:
Write comments

To know more about the problem, give us your valuable commment. We'll try to help you. Thanks

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.