-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 5.4 (Completed)
202 lines (163 loc) · 4.45 KB
/
Exercise 5.4 (Completed)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
1. (Program) a. Using a do-while statement, write, compile, and run a C++ program to accept
a grade. The program should request a grade continuously as long as an invalid grade is
entered. An invalid grade is any grade less than 0 or greater than 100. After a valid grade has
been entered, your program should display the value of the grade entered.
#include <iostream>
using namespace std;
int main()
{
int grade;
do{
cout << "Enter your grade" << endl;
cin >> grade;
if (grade > 0 && grade <= 100){
cout << grade;
}
} while (grade > 100 || grade < 0);
system("PAUSE");
return 0;
}
b. Modify the program written for Exercise 1a so that the user is alerted when an invalid grade
has been entered.
#include <iostream>
using namespace std;
int main()
{
int grade;
do{
cout << "Enter your grade" << endl;
cin >> grade;
if (grade > 0 && grade <= 100){
cout << grade;
}
else{ cout << "invalid number" << endl; }
} while (grade > 100 || grade < 0);
system("PAUSE");
return 0;
}
c. Modify the program written for Exercise 1b so that it allows the user to exit the program by
entering the number 999.
#include <iostream>
using namespace std;
int main()
{
int grade;
do{
cout << "Enter your grade" << endl;
cin >> grade;
if (grade > 0 && grade <= 100){
cout << grade;
}
else if (grade == 999){
grade = 10;
}
else{ cout << "invalid number" << endl; }
} while (grade > 100 || grade < 0);
system("PAUSE");
return 0;
}
d. Modify the program written for Exercise 1b so that it automatically terminates after five
invalid grades are entered.
#include <iostream>
using namespace std;
int main()
{
int grade;
do{
for (int i = 1; i <= 5; i++){
cout << "Enter your grade" << endl;
cin >> grade;
if (grade > 0 && grade <= 100){
cout << grade;
}
else if (i >= 5){
grade = 20;
}
else{ cout << "invalid number" << endl; }
}
} while (grade > 100 || grade < 0);
system("PAUSE");
return 0;
}
2. (Program) a. Write, compile, and run a C++ program that continuously requests a grade to be
entered. If the grade is less than 0 or greater than 100, your program should print a message
informing the user that an invalid grade has been entered; else, the grade should be added to
a total. When a grade of 999 is entered, the program should exit the repetition loop and compute
and display the average of the valid grades entered.
#include <iostream>
using namespace std;
int main()
{
int grade,total =0;
do{
for (int i = 1; i <= 5; i++){
cout << "Enter your grade" << endl;
cin >> grade;
if (grade > 0 && grade <= 100){
total = total + grade;
cout << total << endl;
}
else if ( grade == 999){
grade = 20;
}
else{ cout << "invalid number" << endl; }
}
} while (true);
system("PAUSE");
return 0;
}
b. Run the program written in Exercise 2a on a computer and verify the program by using
appropriate test data.
Done!!
3. (Program) a. Write, compile, and run a C++ program to reverse the digits of a positive integer
number. For example, if the number 8735 is entered, the number displayed should be 5378.
(Hint : Use a do-while statement and continuously strip off and display the number’s units
digit. If the variable num initially contains the number entered, the units digit is obtained as
(num % 10)). After a units digit is displayed, dividing the number by 10 sets up the number
for the next iteration. Therefore, (8735 % 10) is 5 and (8735 / 10) is 873. The do-while
statement should continue as long as the remaining number is not 0.
#include <iostream>
using namespace std;
int main()
{
int num,rev;
cout << "Enter your number to reverse" << endl;
cin >> num;
do{
rev =(num % 10);
cout << rev;
num = (num / 10);
} while (num !=0);
system("PAUSE");
return 0;
}
b. Run the program written in Exercise 3a on a computer and verify the program by using
appropriate test data.
Done!!
4. (Practice) Repeat any of the exercises in Section 5.3, using a do-while statement rather than
a for statement.
#include <iostream>
using namespace std;
int main(){
double money = 1000;
for (double year= 1; year <= 10; year++){
money = money + .03 * money;
cout << money<< '\n';
}
retur 0;
}
-------------Exercise for Section 5.3 Conversion------------------------------
#include <iostream>
using namespace std;
int main()
{
double money = 1000;
int year = 1;
do{
money = money + .03 * money;
cout << money << '\n';
year++;
} while (year <= 10);
system("PAUSE");
return 0;
}