URI Online Judge Solution 1099 Sum of Consecutive Odd Numbers II using C Programming Language.
Read an integer N that is the number of test cases. Each test case is a line containing two integer numbers X and Y. Print the sum of all odd values between them, not including X and Y.
Input
The first line of input is an integer N that is the number of test cases that follow. Each test case is a line containing two integer X and Y.
Output
Print the sum of all odd numbers between X and Y.
Input Sample | Output Sample |
7
4 5
13 10
6 4
3 3
3 5
3 4
3 8
|
0
11
5
0
0
0
12
|
Solution using C :
#include <stdio.h>
int main() {
int T;
scanf("%d",&T);
while(T--)
{
int x,y,i,sum=0;
scanf("%d%d",&x,&y);
if(x>y)
{
int t = x;
x = y;
y = t;
}
for(i=x+1; i<y; i++)
{
if(i%2 != 0)
sum += i;
}
printf("%d\n",sum);
}
return 0;
}
Comments
Post a Comment