-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevenOddDoWhile.cpp
More file actions
69 lines (60 loc) · 1.7 KB
/
evenOddDoWhile.cpp
File metadata and controls
69 lines (60 loc) · 1.7 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
61
62
63
64
65
66
67
68
69
/*********************************
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
char ans; //y or n
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
do
{
cout << "Do you have a number to check? y or n: ";
cin >> ans; //answer
}while(ans!='Y'&&ans!='y'&&ans!='N'&&ans!='n');
while(ans=='y'||ans=='Y')
{
cout << "Enter a number: ";
cin >> num;
count++;
//check if the number is even or odd
if(num%2)
{
cout << num << " is odd" << endl << endl;
oCount++;
total+=num;
}
else
{
cout << num << " is even" << endl << endl;
eCount++;
total+=num;
}
do
{
cout << "Do you have a number to check? y or n: ";
cin >> ans; //answer
}while(ans!='Y'&&ans!='y'&&ans!='N'&&ans!='n');
}
if(count==0) //The user didn't check number for even or odd.
cout << "\nNo numbers were checked" << endl;
else
{
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;
}