Beruflich Dokumente
Kultur Dokumente
#include <iostream>
using namespace std;
int main()
{
double initial_value;
double final_value;
cout << "Please enter the initial balance: ";
cin >> initial_value;
if (initial_value > 90 )
{
> 0
>= 0
>= 8
< 8
= y = 0
is above 1,000, then the rate is $.45 per KWH. Write a program that
prompts the user for the number of Kilowatt Hours used and then
calculates and prints the total electric bill.
Ans:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter the number of kilowatt hours used: ";
double kwh;
cin >> kwh;
if (kwh <= 1000)
{
double bill1 = .60 * kwh;
cout << "Total electric bill: $" << bill1 << endl;
}
else
{
double bill2 = .45 * kwh;
cout << "Total electric bill: $" << bill2 << endl;
}
system("pause");
return 0;
}
3.2. According to your program in Lab 3.1, how much will it cost for:
900 KWH?
1,754 KWH?
10,000 KWH?
Ans:
1. $540
2. $789.3
3. $4500
4.1. The if/else decision for electric rates that was implemented in
Lab 3.1 can be extended to select from more than two possible outcomes.
The if ... else if ... else syntax is used to select exactly one of
several possible actions.
if (condition1)
// Do something ...
else if (condition2)
// Do something different ...
else
// Do something generic ...
Write a program to compute the electric bill when there are 4 different
rates depending on KWH used:
Usage
Rate
0-999 KWH
$0.60
1000-1499 KWH
$0.45
1500-1999 KWH
$0.40
2000 or more KWH $0.35
Ans:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter the number of kilowatt hours used: ";
double kwh;
cin >> kwh;
if (kwh < 1000)
{
double bill1 = .60 * kwh;
cout << "Total electric bill: $" << bill1 << endl;
}
else if ((kwh >= 1000) && (kwh < 1500))
{
double bill2 = .45 * kwh;
cout << "Total electric bill: $" << bill2 << endl;
}
else if ((kwh >= 1500) && (kwh < 2000))
{
double bill3 = .40 * kwh;
cout << "Total electric bill: $" << bill3 << endl;
}
else
{
double bill4 = .35 * kwh;
cout << "Total electric bill: $" << bill4 << endl;
}
system("pause");
return 0;
}
7.
/*
*/
int main()
{
int nyear;
int millennium = 3000;
int years = 0;
cout << "Please enter the current year";
cin >> nyear;
while (nyear != millennium)
{
nyear++;
years++;
}
cout << " Another " << years << " years to the millennium." << "\n";
return 0;
Ans: I found that the while loop iterates one last time at nyears =
2999 and terminates when the number stored in memory is 3000.
Note: The operator,!=, compares two values, and if the values are not
equal, then the program deems it true. So, when I entered 3001, I
expected iterations to continue because 3001 is not equal to 3000, but
the program didnt display anything.
9.1. A variable that counts the iterations of a loop is called a loop
index. Looping can be accomplished with a loop index and a while loop
or we can use a for loop, with the following syntax:
for (loop_index = start_value; condition; index_increment)
Write a program controlled by two for loops that will produce the
following listing of inclusive dates, from the 5th Century B.C. through
the 5th Century A.D.
Century
Century
Century
Century
Century
Century
Century
Century
Century
Century
5
4
3
2
1
1
2
3
4
5
BC
BC
BC
BC
BC
AD
AD
AD
AD
AD
400-499
300-399
200-299
100-199
1-99
1-99
100-199
200-299
300-399
400-499
Ans:
#include <iostream>
using namespace std;
int main()
{
int year1 = 400;
for (int a = 5; a >= 1; a--)
{
int year2 = year1 + 99;
if (a == 1)
{
year1 = 1;
cout << "Century " << a << " BC " << year1 << "-" << year2 <<
endl;
cout << "Century " << a << " AD " << year1 << "-" << year2
<< endl;
}
else
{
endl;
cout << "Century " << a << " BC " << year1 << "-" << year2 <<
year1 = year1 - 100;
year2 = year1 + 99;
}
}
year1 = 100;
int year2 = 199;
for (int a = 2; a <= 5; a++)
{
cout << "Century " << a << " AD " << year1 << "-" << year2 << endl;
year1 = year1 + 100;
year2 = year2 + 100;
}
system("pause");
}
10.1. One loop type might be better suited to a purpose than another.
The following usages are idiomatic.
for
while
do while
*/
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
int n = 1;
while (n != 0)
{
<< "\n";
}
return 0;
}
Ans:
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
int n = 1;
do
{
12.1.
sum = sum + n;
cout << "Sum = " << sum << "\n";
} while (n != 0);
return 0;
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 10; i++)
{
cout << i << " squared equals " << i * i << "\n";
}
return 0;
}
Ans:
#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 10)
{
cout << i << " squared equals " << i * i << "\n";
i++;
}
system("pause");
2
4
6
3
6
9
4
8
12
5
10
15
Ans:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
if (i == 5)
{
int product = 1 * i;
cout << product;
}
else
{
int product = 1 * i;
cout << product << setw(6);
}
}
cout << endl;
for (int i = 1; i <= 5; i++)
{
if (i == 5)
{
int product = 2 * i;
cout << product;
}
else
{
int product = 2 * i;
cout << product << setw(6);
}
}
cout << endl;
for (int i = 1; i <= 5; i++)
{
if (i == 5)
{
int product = 3 * i;
cout << product;
}
else
{
int product = 3 * i;
cout << product << setw(6);
}
}
cout << endl;
system("pause");
14. Write a program that asks the user to enter a salary and adds this
value to a departmental salary. The program should ask the user to enter
-1 when done entering data.
Hint: use bool more = true; to keep entering data. When the user enters
-1
if (salary == -1) more becomes false and the program sums the
total salaries and out puts Departmental Total Salary =
Ans:
#include<iostream>
using namespace std;
int main()
{
double sum = 0;
bool more = true;
while (more)
{
cout << "Enter a salary: ";
double salary;
cin >> salary;
if (salary == -1)
{
more = false;
cout << "Departmental Total Salary: " << sum << endl;
}
sum = sum + salary;
}
system("pause");
}