-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevenOdd.cpp
More file actions
60 lines (53 loc) · 1.66 KB
/
evenOdd.cpp
File metadata and controls
60 lines (53 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*********************************
James Bertel
CS111
Lab 4-1 / 4-2
12-2-17
This program will read an unknown number of positive or negative numbers from the keyboard and output "even" if the number is even or "odd" if it is odd. The user will enter a zero when there are no more numbers to input. The program should display the total number of even numbers, the total number of odd numbers and the average number in the end.
*********************************/
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
//variables
int num;
int count=0; //total amount of numbers
int eCount=0; //total amount of even numbers
int oCount=0; //total amount of odd numbers
int total=0; //total of added numbers
//input the first number
cout << "Enter a number: ";
cin >> num;
if(num==0) //The user didn't check number for even or odd. She entered a zero for the first input.
cout << "\nNo numbers were checked" << endl;
else
{
while(num!=0) //break out when the user enters a zero
{
//To calculate the average number after the while loop, you need to do something here
count++;
//check if the number is even or odd
if(num%2)
{
cout << num << " is odd" << endl;
oCount++;
total+=num;
}
else //num%1
{
cout << num << " is even" << endl;
eCount++;
total+=num;
}
//input the next number
cout << "Enter a number: ";
cin >> num;
}
cout << endl;
cout << eCount << " even numbers" << endl;
cout << oCount << " odd Numbers" << endl;
cout << "The average is " << fixed << setprecision(2) << (double)total/count << endl;
}
return 0;
}