The program asks the users for numbers until the total of the numbers is greater than 30. Also, the user has to count how many numbers are even and how many are odd. I can get the first part of the problem but i am having trouble with the counting part.
i.e. Total is 0 Please enter an integer: 20 Total is 20 You had 1 even numbers and 0 odd numbers.
#include <iostream>
#include <string>
using namespace std;
int main (){
int integer;
int total = 20;
int even_count = 0;
int odd_count = 0;
cout << "Total is 0" << endl;
cout << "Please enter an integer: ";
cin >> integer;
cout << integer << endl;
while ( total <= 30){
cout << "Total is " << total << endl;
cout << "Please enter an integer: ";
cin >> integer;
cout << integer << endl;
total = integer + total;
}
if (integer % 2 == 0) {
even_count = even_count + 1;
}
if (integer % 2 != 0){
odd_count = odd_count + 1;
}
cout << "You had " << even_count << " even numbers and ";
cout << odd_count << " odd numbers.";
cout << endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire