Sum of all odd numbers between a inputed range

I wrote a program to find the sum of all odd numbers between two inputed number (as range).

It shows the sum 0 for every range. Why? What is the problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int t;
    cin>>t;

    while(t--)
    {
        int a,b,sum=0;
        cin>>a>>b;

        int ma=max(a,b);
        int mi=min(a,b);

        for(int i=mi; i<ma; i++)
        {
            if(mi%2!=0)
                sum+=mi;
        }

        cout<<sum<<endl;

    }

    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
// sum is set to 0 every iteration of the loop
while(t--)
{
    int a,b,sum=0;
...}
...
// What determines the value of mi? 
for(int i=mi; i<ma; i++)
{
    if(mi%2!=0)
        sum+=mi;
}
What you said is not clear to me..
this is the problem link : https://www.urionlinejudge.com.br/judge/en/problems/view/1099

Please see now.
closed account (48T7M4Gy)
First of all you need to cout some user prompts e.g

cout << "How many tests? ";
cin >> t;

Then do the same for a and b.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int t;
    cout << "How many tests? ";
    cin>>t;

    while(t--)
    {
        int a,b,sum=0;

        cout << "Enter start: " << endl;
        cin >> a;
        cout << "Enter end: " << endl;
        cin >> b;

        int ma=max(a,b);
        int mi=min(a,b);

        for(int i=mi; i<ma; i+=2)
        {
                sum+=mi;
        }

        cout<<sum<<endl;
    }
    return 0;
}
Last edited on
You need to check and add i, not mi
closed account (48T7M4Gy)
Excellent point ...
Topic archived. No new replies allowed.