URI Online Judge | 1071
Sum of Consecutive Odd Numbers I
Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
Read two integer values X 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 Input | Sample 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 commentsTo know more about the problem, give us your valuable commment. We'll try to help you. Thanks