1158 Sum of Consecutive Odd Numbers III

SemiColon
By -
0
URI Online Judge | 1158

Sum of Consecutive Odd Numbers III

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 odd numbers from 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 that is the number of test cases that follow. Each test case is a line containing two integer and Y.

Output

Print the sum of all consecutive odd numbers from X.
Input SampleOutput Sample
2
4 3
11 2
21
24


Solution:
#include<stdio.h>
int main()
{
    int n,i=1,j,X,Y,sum;

    scanf("%d",&n);

    for(i = 0;i < n;i++){
        sum = 0;
        scanf("%d %d",&X,&Y);

        if(X%2==0)
            X++;

        for(j = 0;j < Y;j++){
            sum += X;
            X += 2;;
        }
        printf("%d\n",sum);
    }
    return 0;
}

Tags:

Post a Comment

0Comments

Post a Comment (0)