| | |
C++ Calculating even Values Problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2006
Posts: 11
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2006
Posts: 11
Reputation:
Solved Threads: 0
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;
}
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;
}
•
•
Join Date: Feb 2006
Posts: 50
Reputation:
Solved Threads: 5
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[i];
if(x[i]%2==0) // checks if the nubmer is even or not
{ sum=sum+x[i];
}
}
cout<<sum;
Hope this helps
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[i];
if(x[i]%2==0) // checks if the nubmer is even or not
{ sum=sum+x[i];
}
}
cout<<sum;
Hope this helps
•
•
Join Date: Apr 2006
Posts: 11
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Apr 2006
Posts: 6
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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:
•
•
Join Date: Jan 2008
Posts: 1
Reputation:
Solved Threads: 0
cpp Syntax (Toggle Plain Text)
#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; }
Last edited by WolfPack; Jan 19th, 2008 at 9:53 am. Reason: Added [CODE=CPP][/CODE] tags
![]() |
Similar Threads
- Passing Form Values Problem (ASP)
- Color change problem (Java)
- Problem passing value from radio button to second page. (PHP)
Other Threads in the C++ Forum
- Previous Thread: how to delete some text from a txt file??
- Next Thread: Please help in this program array
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






