Guys I need help. can you help me determine what are the things that i'll used to run my program like this:

Enter 5 numbers but refuse to display if the value is an odd number. Display only even numbers and the sum of all even number.

I'm not asking for the whole thing but the idea only. Hoping for your kindness. thanks

Dani AI

Generated

Two compact, practical options address the original request. Reading all five values into a container (as suggested) makes it trivial to filter, sort, or reprint later; processing each value as it is entered (as suggested) uses constant memory and can show evens immediately. Parity-check techniques were already raised by and ; favor clarity over micro-optimizations as advised — modern compilers handle most low-level tweaks. The example below concentrates on input safety and a safe summation strategy.

Checklist before coding:

  • Verify extraction succeeded (check the stream state) so non-integer input is handled cleanly.
  • Use a wide type for values and the accumulator (use long long for most cases).
  • Decide whether to store evens (needed only if reprinting or further processing) or to print/accumulate on the fly.
  • Treat negative values consistently (parity still applies).
  • Check for overflow when summing many or large values.

A short, safe C++ example that reads five integers, keeps only evens, prints them and reports their sum:

#include <iostream>
#include <vector>
#include <limits>

inline bool is_even(long long n) { return (n / 2) * 2 == n; }

int main() {
    const int count = 5;
    std::vector<long long> evens;
    long long x;

    for (int i = 0; i < count; ++i) {
        if (!(std::cin >> x)) {
            std::cerr << "bad input\n";
            return 1;
        }
        if (is_even(x)) evens.push_back(x);
    }

    long long sum = 0;
    std::cout << "Even numbers:";
    for (long long v : evens) {
        std::cout << ' ' << v;
        if (v > 0 && sum > std::numeric_limits<long long>::max() - v) {
            std::cerr << "\nsum overflow\n";
            return 2;
        }
        if (v < 0 && sum < std::numeric_limits<long long>::min() - v) {
            std::cerr << "\nsum underflow\n";
            return 3;
        }
        sum += v;
    }
    std::cout << "\nSum: " << sum << '\n';
    return 0;
}

A streaming variant simply prints each even and updates a running total inside the input loop; storing is needed only when later reprinting or reprocessing is required. Common failures come from unvalidated input, mixing input methods without clearing the stream, and using too-small types for the accumulator.

Recommended Answers

All 7 Replies

Read(input) all the numbers into an array.
Process the array.

But ...

do not really need to use an array, if it's ok to process and display the even numbers as they are entered :)

... we both seem to be stuck on the night shift +1 :)

You can check for even numbers using the % operator on an int:

int even = 2, odd = 5;
std::cout << "even: " << even % 2 << " odd: " << odd % 2 << std::endl;

any even number will return 0 when you do % 2 to it, while odd numbers will return 1. You can use this in an if statement to do things with only one of the kinds (even or odd) of number.

: this also works with even & 1 This will also return 0 if even is even, 1 if even is odd.

: very true :)

Do you know if that's more efficient? I suspect it is; unless there's some cunning way of implementing mod, or something like that...

I would think and decent compiler would make even % 2 and even & 1 the same operation. My rule of thumb is write clear and legible code. This will help with maintainability and will generally allow the optimizer to take care of most optimizations that can been done. mike_2000_17 wrote a very nice guide showing how you can really get a nice performance increase in your code without using any sort of micro optimizations.

Thanks for all the replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.