简体   繁体   中英

C++ : Count even / odd numbers in a range

My program has to count how many numbers in a range are even and how many of them are odd but I can't seem to figure it out.It kinda works but when I put numbers in it spouts out nonsense. I'm an extreme nooob when it comes to programing, I think that the problem has to be at line 21 for (i=n; i<=m; i++) { ? But I'm not sure. I have a programing book but it does not help much,maybe someone can help?

#include <iostream>

using namespace std;

int main()
{
    int n;
    int m;
    int i;
    int a;
    int b;

    cout << "Enter a number that begins interval: ";
    cin >> n;
    cout << "Enter a number that ends interval: ";
    cin >> m;
    a=0;
    b=0;


    for (i=n; i<=m; i++) {
        if (i%2 == 0){
                a=a+i;
    }
    else {
          b=b+i;
    }
}
    cout << " unequal numbers: " << a << endl;
    cout << "  equal  numbers: " << b << endl;

Assuming you mean even and odd numbers your problem lies in this code:

for (i=n; i<=m; i++) {
    if (i%2 == 0){
        a=a+i;   // increase number of even numbers by i
    }
    else {
        b=b+i;   // increase number of odd numbers by i
    }
}

What you might want do to do is add 1 (instead of whatever i is):

 for (i = n; i <= m; ++i) {
    if (i % 2 == 0)
        ++a;   // increase number of even numbers by one
    else 
        ++b;   // increase number of odd numbers by one
 }

Also I'd suggest using better variable names, for example even and odd instead of a and b and so on. It makes code easier to understand for everybody, even for you.

Just a little more tips. Assigning variables as soon as you declare them is good practice:

int m = 0;

You can declare variable inside of for loop, and in your case there is no need to declare it out of it:

for (int i = n; i <= m; ++i) { ... }

Example how it can change look and clarity of your code:

#include <iostream>

using namespace std;

int main() {
    int from = 0,
        to   = 0,
        even = 0,
        odd  = 0;

    cout << "Enter a number that begins interval: ";
    cin >> from;

    cout << "Enter a number that ends interval: ";
    cin >> to;

    for (int i = from; i <= to; ++i) {
        if (i % 2 == 0)
            ++even;
        else 
            ++odd;
    }

    cout << " even numbers: " << even << endl;
    cout << " odd numbers: " << odd << endl;

    return 0;  // don't forget this! main is function returning int so it should return something
}

Ok, so as per the new clarification the following should work

#include <iostream>

using namespace std;

int main()
{
int n;
int m;
int i;
int a;
int b;

cout << "Enter a number that begins interval: ";
cin >> n;
cout << "Enter a number that ends interval: ";
cin >> m;
a=0;
b=0;


for (i=n; i<=m; i++) {
    if (i%2 == 0){
        a++;
    }else {
        b++;
    }
}

cout << " unequal numbers: " << a << endl;
cout << "  equal  numbers: " << b << endl;
}

So the following changes were done:

  • The for loop was closed
  • a = a + i or b = b + i was wrong as you are adding the counter value to the count which should be a++ or b++. Changed that also
  • The last two lines where you are showing your result was out of the main method, brought them inside the main method
  • Hope you find this useful.

You don't need to use loop to count even and odd numbers in a range.

#include <iostream>

int main ()
{
    int n,m,even,count;
    std::cin >> n >> m;
    count=m-n+1;
    even=(count>>1)+(count&1 && !(n&1));
    std::cout << "Even numbers: " << even << std::endl;
    std::cout << "Odd numbers: " << count-even << std::endl;
}
    #include <iostream>
    using namespace std;
    int main()
    {
        int n, i;
        cin >> n;
        cout << " even : ";
        for (i = 1; i <= n * 2; i++)
        {
            if (i % 2 == 0)
                cout << i << " ";
        }
        cout << " odd : ";
        for (i = 1; i <= n * 2; i++)
        {
            if (i % 2 != 0)
    
                cout << i << " ";
        }
        return 0;
    }

//input n = 5 
// output is  even : 2 4 6 8 10
 //             odd : 1 3 5 7 9 
#include <iostream>

using namespace std;

int main()
{
    int n;
    int m;
    int i;
    int a;
    int b;

    cout << "Enter a number that begins interval: ";
    cin >> n;
    cout << "Enter a number that ends interval: ";
    cin >> m;
    a = 0;
    b = 0;


    for (i = n; i < = m; i++) {
        if (i%2 == 0){
            a = a + 1;
        } else {
            b = b + 1;
        }
    }

    cout << " unequal numbers: " << a << endl;
    cout << "  equal  numbers: " << b << endl;
}

Not sure why you are looping through all the elements (half of them are going to be even and the other half odd). The only case where you have to consider when the interval length is not divisible by two.

using namespace std;

int main()
{
    int n;
    int m;
    int x;
    int odds;
    int evens;
    cout << "Enter a number that begins interval: ";
    cin >> n;
    cout << "Enter a number that ends interval: ";
    cin >> m;
  
    cout << n << " " << m << endl;
    x = m - n + 1;
    odds = x / 2;
    evens = odds;
    
    if (x % 2 != 0) {
        if (n % 2 == 0) {
            evens++;
        } else {
            odds++;
        }
    }

    cout << " even numbers: " << evens << endl;
    cout << " odd  numbers: " << odds << endl;
}

This is a more readable version of @Lassie's answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM