Wednesday, April 6, 2016

URI 1071 Solution in java language


URI Online Judge | 1071

Sum of Consecutive Odd Numbers I

Adapted by Neilor Tonin, URI  Brazil
Timelimit: 1
Read two integer values and Y. Print the sum of all odd values between them.

Input

The input file contain two integer values.

Output

The program must print an integer number. This number is the sum off all odd values between both input values that must fit in an integer number.
Sample InputSample Output
6
-5
5
15
12
13
12
12
0

URI 1071 Solution in java language:

import java.util.Scanner;

public class URI_1071 {

  public static void main(String[] args) {
   int X, Y, total = 0;
   Scanner input =new Scanner(System.in);
   X = input.nextInt();
   Y = input.nextInt();

   if (X > Y) {
    for (int i = X - 1; i > Y; i--) {
     if (i % 2 != 0) {
      total += i;
     }
    }
   }else {
    for (int i = Y - 1; i > X; i--) {
     if (i % 2 != 0) {
      total += i;
     }
    }
   }

   System.out.print(total+"\n");

  }

}



No comments:
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.