URI Online Judge Solution 1158 Sum of Consecutive Odd Numbers III using C, Python Programming Language.
Read an integer N that is the number of test cases that follows. Each test case contains two integers X and Y. Print one output line for each test case that is the sum of Y odd numbers from X including it if is the case. For example:
for the input 4 5, the output must be 45, that is: 5 + 7 + 9 + 11 + 13
for the input 7 4, the output must be 40, that is: 7 + 9 + 11 + 13
Input
The first line of the 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 consecutive odd numbers from X.
Input Sample | Output Sample |
2 | 21 |
URI 1158 Solution in C:
#include <stdio.h>
int main() {
int T,x,y,s;
scanf("%d",&T);
while(T--)
{
s=0;
scanf("%d%d",&x,&y);
while(y)
{
if(x%2!=0)
{
s += x;
x++;
y--;
}
else
{
x++;
}
}
printf("%d\n",s);
}
return 0;
}
Comments
Post a Comment