[URI Online Judge] – 1071 – Sum of Consecutive Odd Numbers I

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

Solution:

#include <iostream>
int main(){
 int i, x, y, s = 0;
std::cin >> x;
 std::cin >> y;
if (x < y){
 for (i = x + 1; i < y; i++){
 if (i % 2 != 0) s += i;
 }
 } else {
 for (i = y + 1; i < x; i++){
 if (i % 2 != 0) s += i;
 }
 }
std::cout << s << std::endl;
return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s