C++ Calculating even Values Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2006
Posts: 11
Reputation: tuannie is an unknown quantity at this point 
Solved Threads: 0
tuannie tuannie is offline Offline
Newbie Poster

C++ Calculating even Values Problem

 
0
  #1
Apr 1st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ Calculating even Values Problem

 
0
  #2
Apr 1st, 2006
Have you tried anything yet? We tend to expect at least a tiny amount of code that proves you've made an honest attempt.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 11
Reputation: tuannie is an unknown quantity at this point 
Solved Threads: 0
tuannie tuannie is offline Offline
Newbie Poster

Re: C++ Calculating even Values Problem

 
0
  #3
Apr 1st, 2006
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;
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 50
Reputation: ultra vires is an unknown quantity at this point 
Solved Threads: 5
ultra vires ultra vires is offline Offline
Junior Poster in Training

Re: C++ Calculating even Values Problem

 
0
  #4
Apr 2nd, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 11
Reputation: tuannie is an unknown quantity at this point 
Solved Threads: 0
tuannie tuannie is offline Offline
Newbie Poster

Re: C++ Calculating even Values Problem

 
0
  #5
Apr 2nd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: C++ Calculating even Values Problem

 
0
  #6
Apr 2nd, 2006
Just a rearrangement and modification of your original code. Look and learn.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 6
Reputation: viet_mafia is an unknown quantity at this point 
Solved Threads: 0
viet_mafia viet_mafia is offline Offline
Newbie Poster

Re: C++ Calculating even Values Problem

 
0
  #7
Apr 11th, 2006
  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:
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 1
Reputation: khanh1975 is an unknown quantity at this point 
Solved Threads: 0
khanh1975 khanh1975 is offline Offline
Newbie Poster

Re: C++ Calculating even Values Problem

 
0
  #8
Jan 19th, 2008
  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC