944,214 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4892
  • C++ RSS
Apr 1st, 2006
0

C++ Calculating even Values Problem

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tuannie is offline Offline
11 posts
since Apr 2006
Apr 1st, 2006
0

Re: C++ Calculating even Values Problem

Have you tried anything yet? We tend to expect at least a tiny amount of code that proves you've made an honest attempt.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 1st, 2006
0

Re: C++ Calculating even Values Problem

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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tuannie is offline Offline
11 posts
since Apr 2006
Apr 2nd, 2006
0

Re: C++ Calculating even Values Problem

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
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
ultra vires is offline Offline
50 posts
since Feb 2006
Apr 2nd, 2006
0

Re: C++ Calculating even Values Problem

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tuannie is offline Offline
11 posts
since Apr 2006
Apr 2nd, 2006
0

Re: C++ Calculating even Values Problem

Just a rearrangement and modification of your original code. Look and learn.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int count = 1, number ;
  6. int Total = 0 ;
  7. while (count <= 5)
  8. {
  9. cout << "Please input interger " << count << " : ";
  10. cin >> number ;
  11. if ( number % 2 == 0 )// % is the Modulus, it gives the remainder after dividing by 2.
  12. {
  13. Total = Total + number ;
  14. }
  15. }
  16. cout << "Even Number Total is " << Total << endl ;
  17. system("pause");
  18. return 0;
  19. }
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Apr 11th, 2006
0

Re: C++ Calculating even Values Problem

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. const int MAXNUMS = 5;
  6. int count;
  7. int num, total;
  8.  
  9. cout << "\nThis program will ask you to enter " << MAXNUMS << " numbers.\n";
  10. count = 1;
  11. total = 0;
  12. while (count <= MAXNUMS)
  13. {
  14. cout << "Please input integer " << count << " : ";
  15. cin >> num;
  16. if ( num % 2 == 0 )// % is the Modulus, it gives the remainder after dividing by 2.
  17. {
  18. total = total + num ;
  19. }
  20. count++;
  21. }
  22. cout << "Even Number Total is " << total << endl ;
  23.  
  24. system("pause");
  25. return 0;
  26. }

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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
viet_mafia is offline Offline
6 posts
since Apr 2006
Jan 19th, 2008
0

Re: C++ Calculating even Values Problem

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7. int num;
  8. cout << "enter a number: ";
  9. cin >> num;
  10. int count = 0;
  11. int sum = 0;
  12. do
  13. {
  14. if(num % 2 == 0)
  15. sum += num;
  16. count++;
  17. cout << "enter a number again: ";
  18. cin >> num;
  19. }while(count < 5);
  20. cout << sum << endl;
  21. return 0;
  22. }
Last edited by WolfPack; Jan 19th, 2008 at 9:53 am. Reason: Added [CODE=CPP][/CODE] tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khanh1975 is offline Offline
1 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to delete some text from a txt file??
Next Thread in C++ Forum Timeline: Please help in this program array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC