Uri/beecrowd problem no - 1099 solution in C
#include <stdio.h>
int main() {
int N, X, Y, i, j, temp, sum;
scanf("%d", &N);
for (i = 0; i < N; i++) {
scanf("%d %d", &X, &Y);
if (X > Y) {
temp = X;
X = Y;
Y = temp;
}
sum = 0;
for (j = X+1; j < Y; j++) {
if (j % 2 != 0) {
sum += j;
}
}
printf("%d\n", sum);
}
return 0;
}
Explanation:
This program solves the Uri/beeCrowd problem number 1099. The problem requires finding the sum of all odd numbers between two given numbers, excluding those two numbers.
We start by including the necessary header file
stdio.h
for input/output operations.The
main
function is defined where the program execution begins.We declare variables
N
,X
,Y
,i
,j
,temp
, andsum
as integers.N
will store the number of test cases.X
andY
will store the two numbers between which we need to find the sum of odd numbers.i
andj
are used as loop counters.temp
is a temporary variable used for swapping values.sum
will store the sum of all odd numbers betweenX
andY
.We use
scanf
function to read the number of test casesN
from the user.We start a
for
loop to iterateN
times, as each test case requires finding the sum of odd numbers between two given numbers.Inside the loop, we use
scanf
to read the two numbersX
andY
from the user.We check if
X
is greater thanY
. If it is, we swap the values ofX
andY
using the temporary variabletemp
. This ensures thatX
always holds the smaller number andY
holds the larger number.We initialize the variable
sum
to 0.We start another
for
loop with the variablej
ranging fromX+1
toY-1
. This loop iterates over all the numbers betweenX
andY
, excludingX
andY
.Inside the loop, we check if
j
is an odd number using the conditionj % 2 != 0
. If it is, we addj
to the variablesum
.After the inner loop finishes, we print the value of
sum
usingprintf
.The outer loop continues until all the test cases are processed.
Finally, we return 0 to indicate successful program execution.
This program follows a straightforward approach to solve the problem by using nested loops and conditional statements. The outer loop iterates over the given number of test cases, and the inner loop calculates the sum of odd numbers between the given range.