Start New Discussion within our Computer Science Community

write a program to read a seuence of numbers terminated by 0 and print the sum of all the even numbers.
How would you write the algorithm/pseudocaode for this

Do you have any code that you have tested? Because the odds of us doing it for is slim rather than us assisting you in building it...

This is not complicated at all, THINK about it!

1) Input values into an array that are above "0":

input = {1, 2, ..., 2, 3, .. 9,> 0};

2) Check to see if input (at the given time) is "0"

   if this value is not greater than 0 
       check to see if the values of the array are even
       if true, then->
          initalise sum = 0;
          sum += value[x] // x representing the location at the given time

Something like this =) Here, look at this example code, to give you a clear understanding:

#include <iostream>
#include <vector>

using namespace std;

double summationOfEven(vector<int> &nums)
    int sum = 0;

    for(unsigned i=0; (i < nums.size()); i++)
        if(nums[i] % 2 == 0)
            sum += nums[i];

    return sum;
int main(int argc, char *argv[]) {

    vector<int> numbers;

    bool isSet = false;

        int num = 0;

        cout << "Please enter a number: ";
        cin >> num;


        if(num == 0)
          isSet = true;

    cout << summationOfEven(numbers);


Please enter a number: 2
Please enter a number: 4
Please enter a number: 6
Please enter a number: 8
Please enter a number: 5
Please enter a number: 0

This article has been dead for over six months. Start a new discussion instead.