Hey everyone,

I am new to C++ and I was wondering if anyone could help me start off or by guiding me to which method of steps I should take first in solving the following problem:

write a c++ program that with a loop structure that reads in 5 intergers one by one,
calculate the sum of the even values that are read and display the final result.

Any suggestion or comments in regards to the starting point of this problem would be much apprieciated.

Have you tried anything yet? We tend to expect at least a tiny amount of code that proves you've made an honest attempt.

Yeh I have attempted of the code, but I seem to can't get the code to work here is attempted code:

int main(){
   int count = 1, FirstNum = 0, SecondNum = 0, ThirdNum = 0, FourthNum = 0, FifthNum = 0;
   int FirstTotal, SecondTotal , ThirdTotal , FourthTotal, FifthTotal, SumTotal;
   bool EvenNum = 1;

    cout << "Please input a interger: ";
    cin >> FirstNum >> SecondNum >> ThirdNum >> FourthNum >> FifthNum;

    FirstTotal = FirstNum % 2;
    SecondTotal = SecondNum % 2;
    ThirdTotal = ThirdNum % 2;
    FourthTotal = FourthNum % 2;
    FifthTotal = FifthNum % 2;

 if (count >=1)
  {
    while (count <= 5)
      {
        if (FirstTotal == EvenNum || SecondTotal == EvenNum || ThirdTotal == EvenNum || FourthTotal == EvenNum || FifthTotal == EvenNum)
           {
             SumTotal = FirstNum + SecondNum + ThirdNum + FourthNum + FifthNum;
           }       
      }
  }
system("pause");
return 0;
}

Edited 3 Years Ago by pyTony: fixed formatting

try using an array instead of making 5 variable...

if you still dont get it then read on for the code...


int x[5],sum=0; // creates an array to store 5 integers

for(int i=0;i<5;i++) // loop to take 5 values and evaluate them
{
cin>>x;
if(x%2==0) // checks if the nubmer is even or not
{ sum=sum+x;
}
}
cout<<sum;

Hope this helps ;)

I haven't been shown using an array as yet. I have only been shown the following techniques, and they are:
1. If statement, nested statement (i.e else if) and briefly on the following loop (for, while and do) as well as a bit of increment i.e number++

Could some one please suggest a technique I could use? i.e. for or while loop or something.

Just a rearrangement and modification of your original code. Look and learn.

#include <iostream>
using namespace std;
int main()
{
    int count = 1, number ; 
    int Total = 0 ;
    while (count <= 5)
    {
        cout << "Please input  interger " << count << " : "; 
        cin >> number ;
        if ( number % 2 == 0 )// % is the Modulus, it gives the remainder after dividing by 2.
        {
              Total = Total + number ;
        }
    }
    cout << "Even Number Total is " << Total << endl ;
    system("pause");
    return 0;
}
#include <iostream>
using namespace std;
int main()
{
    const int MAXNUMS = 5;
    int count;
    int num, total;

cout << "\nThis program will ask you to enter " << MAXNUMS << " numbers.\n";
count = 1;
total = 0;
    while (count <= MAXNUMS)
    {
        cout << "Please input integer " << count << " : "; 
        cin >> num;
        if ( num % 2 == 0 )// % is the Modulus, it gives the remainder after dividing by 2.
        {
              total = total + num ;    
        }
    count++;
}
    cout << "Even Number Total is " << total << endl ;   
             
    system("pause");
    return 0;
}

this should help you out, only calculates the even numbers out of 5 inputs, displaying the total of the even numbers at the end.

you can thank me later :mrgreen:

#include <iostream>
using namespace std;

int main()
{
	
	int num;
	cout << "enter a number: ";
	cin >> num;
	int count = 0;
	int sum = 0;
	do
	{
		if(num % 2 == 0)
		sum += num;
		count++;
		cout << "enter a number again: ";
		cin >> num;
	}while(count < 5);
	cout << sum << endl;
	return 0;
}
This article has been dead for over six months. Start a new discussion instead.