Question

Write a function named totalEvens that computes the total number of even integers that are less...

Write a function named totalEvens that computes the total number of even integers that are less than a given threshold. The function takes one parameter: 1. threshold, an integer The function repeatedly asks the user to enter a number until -1 is entered. The function returns the total number of even integers less then threshold. For example, if threshold is 10 and user enters: 5 4 7 20 12 -1 The function would return 1. (USE C++)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int totalEvens(int threshold) {
    int count = 0, num;
    while (true) {
        cout << "Enter a number(-1 to exit): ";
        cin >> num;
        if (num == -1) break;
        if (num < threshold && num % 2 == 0) {
            count++;
        }
    }
    return count;
}

int main() {
    int t;
    cout << "Enter a threshold value: ";
    cin >> t;
    int n = totalEvens(t);
    cout << "Number of evens entered is " << n << endl;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Write a function named totalEvens that computes the total number of even integers that are less...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT