S. Sum of Consecutive Odd Numbers Using C

šŸ” Problem Solving from Codeforces using C language

This is a problem-solving post from Codeforces. Below is the problem with a short explanation and its solution.

This example is solved using C language.

🧩 Main Problem: Given two numbers X and Y. Print the sum of all odd numbers between them, excluding X and Y.

šŸ”— Problem Link: Codeforces Problem

šŸ’” Solution:

#include <stdio.h>
int main()
{
    int t, x, y;
    scanf("%d", &t);
    while (scanf("%d %d", &x, &y) != EOF) //EOF means End Of File,, that means the datas will scan under scanf is till the ending of input file.
    {                   // if we dont use EOF it may hamper on our code.
        if (x > y)
        {               // here, we sorting/rearranging the numbers if there is ani number greater than nextone
            int temp = x; // vale of x is in temp
            x = y;  // vale of y is in x (that means, y is now x)
            y = temp; // stored value of x in temp now is assigned in y (x is now y)
        }
        int sum = 0;
        for (int a = x + 1; a < y; a++) // we are excluding x and y
        {
            if (a % 2 != 0)
                sum = sum + a;  // calculation the summation value 
        }
        printf("%d ", sum);
        printf("\n");
    }
    return 0;
}

āœ… This solution takes input from user about how many test case are there then it takes those test cases. then it sort numbers od testcases from minimum to maximum then find the odd numbers between the test values and then summation them.

šŸ“ Input:

3
5 6
10 4
4 9

šŸ“„ Output:

0
21
12

šŸ”— Find Similar Problems from Codeforces– Click Here

šŸ¤” More Problem Solving – Click Here

šŸ“˜ Learn C Programming ā€“ Click Here

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top