Problem :
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 |
Solution:
#include<stdio.h>
int
main()
{
int
x , y, sum=0, i;
scanf
(
"%d %d"
,&x,&y);
if
(x<y){
for
(i=x+1; i<y; i++){
if
(i%2!=0)
sum+=i;
}
}
else
if
(x>y){
for
(i=y+1; i<x; i++){
if
(i%2!=0)
sum+=i;
}
}
printf
(
"%d\n"
,sum);
return
0;
}
No comments:
Post a Comment