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.

  1. We start by including the necessary header file stdio.h for input/output operations.

  2. The main function is defined where the program execution begins.

  3. We declare variables N, X, Y, i, j, temp, and sum as integers.

  4. N will store the number of test cases.
  5. X and Y will store the two numbers between which we need to find the sum of odd numbers.
  6. i and j are used as loop counters.
  7. temp is a temporary variable used for swapping values.
  8. sum will store the sum of all odd numbers between X and Y.

  9. We use scanf function to read the number of test cases N from the user.

  10. We start a for loop to iterate N times, as each test case requires finding the sum of odd numbers between two given numbers.

  11. Inside the loop, we use scanf to read the two numbers X and Y from the user.

  12. We check if X is greater than Y. If it is, we swap the values of X and Y using the temporary variable temp. This ensures that X always holds the smaller number and Y holds the larger number.

  13. We initialize the variable sum to 0.

  14. We start another for loop with the variable j ranging from X+1 to Y-1. This loop iterates over all the numbers between X and Y, excluding X and Y.

  15. Inside the loop, we check if j is an odd number using the condition j % 2 != 0. If it is, we add j to the variable sum.

  16. After the inner loop finishes, we print the value of sum using printf.

  17. The outer loop continues until all the test cases are processed.

  18. 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.