C++: a beginner's Guide, Second Edition



Download 11,33 Mb.
Pdf ko'rish
bet46/194
Sana12.03.2022
Hajmi11,33 Mb.
#491693
1   ...   42   43   44   45   46   47   48   49   ...   194
Bog'liq
C A Beginner\'s Guide 2nd Edition (2003)

Step by Step
1.
Create a new file called RegPay.cpp.
2.
Here are the variables that will be used by the program:
double Principal; // original principal
double IntRate; // interest rate, such as 0.075
double PayPerYear; // number of payments per year
double NumYears; // number of years
double Payment; // the regular payment
double numer, denom; // temporary work variables
double b, e; // base and exponent for call to pow()
Notice how each variable declaration is followed by a comment that describes its use. This helps 
anyone reading your program understand the purpose of each variable. Although we won’t include 


30 
C++ A Beginner’s Guide by Herbert Schildt
such detailed comments for most of the short programs in this book, it is a good practice to follow 
as your programs become longer and more complicated.
3.
Add the following lines of code, which input the loan information:
cout << "Enter principal: ";
cin >> Principal;
cout << "Enter interest rate (i.e., 0.075): ";
cin >> IntRate;
cout << "Enter number of payments per year: ";
cin >> PayPerYear;
cout << "Enter number of years: ";
cin >> NumYears;
4.
Add the lines that perform the financial calculation:
numer = IntRate * Principal / PayPerYear;
e = -(PayPerYear * NumYears);
b = (IntRate / PayPerYear) + 1; 
denom = 1 - pow(b, e);
Payment = numer / denom;
5.
Finish the program by outputting the regular payment, as shown here:
cout << "Payment is " << Payment;
6.
Here is the entire RegPay.cpp program listing:
/*
Project 2-3
Compute the regular payments for a loan.
Call this file RegPay.cpp
*/
#include
#include
using namespace std;
int main() {
double Principal; // original principal
double IntRate; // interest rate, such as 0.075
double PayPerYear; // number of payments per year
double NumYears; // number of years


31 
C++ A Beginner’s Guide by Herbert Schildt
double Payment; // the regular payment
double numer, denom; // temporary work variables
double b, e; // base and exponent for call to pow() 
cout << "Enter principal: ";
cin >> Principal;
cout << "Enter interest rate (i.e., 0.075): ";
cin >> IntRate;
cout << "Enter number of payments per year: ";
cin >> PayPerYear;
cout << "Enter number of years: ";
cin >> NumYears;
numer = IntRate * Principal / PayPerYear;
e = -(PayPerYear * NumYears);
b = (IntRate / PayPerYear) + 1;
denom = 1 - pow(b, e);
Payment = numer / denom;
cout << "Payment is " << Payment; 
return 0;
}
Here is a sample run:
Enter principal: 10000
Enter interest rate (i.e., 0.075): 0.075
Enter number of payments per year: 12
Enter number of years: 5
Payment is 200.379
7.
On your own, have the program display the total amount of interest paid over the life of the loan.
1.
What type of integers are supported by C++?
2.
By default, what type is 12.2?


32 
C++ A Beginner’s Guide by Herbert Schildt
3.
What values can a bool variable have?
4.
What is the long integer data type?
5.
What escape sequence produces a tab? What escape sequence rings the bell?
6.
A string is surrounded by double quotes. True or false?
7.
What are the hexadecimal digits?
8.
Show the general form for initializing a variable when it is declared.
9.
What does the % do? Can it be used on floating-point values?
10.
Explain the difference between the prefix and postfix forms of the increment operator.
11.
Which of the following are logical operators in C++?
a.
&&
b.
##
c.
||
d.
$$
e.
!
12.
How can
x = x + 12;
be rewritten?
13.
What is a cast?
14.
Write a program that finds all of the prime numbers between 1 and 100.



Download 11,33 Mb.

Do'stlaringiz bilan baham:
1   ...   42   43   44   45   46   47   48   49   ...   194




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish