2.1( Convert centigrade to Fahrenheit ) Programming , Read in a double The centigrade value of the model , Convert it to Fahrenheit and display the result .
#include <iostream> using namespace std; int main() { double celsius =
0.0;// Centigrade temperature value cout << " Please input the temperature in centigrade :"; cin >> celsius; double fahrenheit =
0.0;// Fahrenheit temperature fahrenheit = (9.0 / 5)*celsius + 32;// Conversion formula cout << celsius <<
" What's the centigrade " << fahrenheit << " Fahrenheit degree "; return 0; }
2.2( Calculate the volume of a cylinder ) Write a program , Read in the radius and length of a cylinder , Calculate its area and volume with the following formula :
area = radius * radius * π;volume = area * length
#include <iostream> #include <cmath> using namespace std; int main() { const
double PI = 3.141592;// Defining constants π double radius = 0.0, length = 0.0; cout << "Enter
the radius and length of a cylinder:"; cin >> radius >> length; double area =
0.0, volume = 0.0; area = pow(radius, 2)*PI;// Formula for calculating cylinder area volume = area *
length;// Formula for calculating cylinder volume cout << "The area is:" << area << endl; cout << "The volume
is:" << volume << endl; return 0; }
2.3( Converting feet to meters ) Write a program , Read in a length in feet , Convert it to a value in meters , Output results .1 Feet equal 0.305 rice .
#include <iostream> using namespace std; int main() { const double rate =
0.305; double feet = 0.0, meter = 0.0; cout << "Enter a value for feet:"; cin
>> feet; meter = rate * feet; cout << feet << " feet is " << meter << "
meters"; return 0; }
2.4( Convert pounds to kilograms ) Programming , Read in a weight in pounds , Convert to a value in kilograms , Output results .1 Pound equals 0.454 Kilogram .
#include <iostream> using namespace std; int main() { const double rate =
0.454; double pound = 0.0, kilogram = 0.0; cout << "Enter a number in pounds:";
cin >> pound; kilogram = pound * rate; cout << pound << " pounds is " <<
kilogram << " kilograms"; return 0; }
2.5( Financial application : Calculating consumption ) Programming , Read in the consumption subtotal and tip rate , Calculate tip amount and total consumption . for example , User input consumption subtotal is 10, The tip rate is 15%, The program should output the tip amount as $1.5 And the total consumption is $11.5.
#include <iostream> using namespace std; int main() { double subtotal = 0.0,
gratuityRate = 0.0; cout << "Enter the subtotal and a gratuity rate:"; cin >>
subtotal >> gratuityRate; double gratuity = 0.0, total = 0.0; gratuity =
gratuityRate / 100 * subtotal; total = gratuity + subtotal; cout << "The
gratuity is $" << gratuity << " and total is $" << total; return 0; }
2.6( All the numbers in an integer are added ) Write a program , Read in a 0~1000 Integer in range , Add all the numbers in this integer . for example , If the integer is 932, Then the sum of all numbers is 14.
#include <iostream> using namespace std; int main() { int number = 0,
sumOfDigits = 0; cout << "Enter a number between 0 and 1000:"; cin >> number;
sumOfDigits += number % 10;// Extract one digit sumOfDigits += (number / 10) % 10;// Extract ten digits
sumOfDigits += (number / 100) % 10;// Extract 100 digits cout << "The sum of the digits is "
<< sumOfDigits; return 0; }
2.7( Find out the number of years ) Programming , Prompt user to enter minutes ( as ,10 100 million ), Output the corresponding number of years and days . For simplicity , Let's say there's one in a year 365 day .
#include <iostream> using namespace std; int main() { int numberOfMinutes = 0;
int numberOfDays = 0; int numberOfYears = 0; cout << "Enter the number of
minutes:"; cin >> numberOfMinutes; numberOfDays = numberOfMinutes / 60 /
24;// How many days are there in total numberOfYears = numberOfDays / 365;// How many years are there in days numberOfDays %=
365;// The surplus of the total number of days to the year is the remaining number of days cout << numberOfMinutes << " minutes is approximately " <<
numberOfYears << " years and " << numberOfDays << " days"; return 0; }
2.8( current time ) Program list 2-9,ShowCurrentTime.cpp, Give a program , Displays the current Greenwich mean time . Modify the program so that it prompts the user to enter a time zone different from Greenwich mean time , Output the time of the pending time zone .
/* This problem will appear when crossing the sky bug, as GMT by 1:0:0, difference -5 Time zones Then the current time is -4:0:0, But according to the meaning and the difficulty , This program is The right answer for the period */
#include <iostream> #include <ctime> using namespace std; int main() { int
totalSecond = time(0); int currentSecond = totalSecond % 60; int currentMinute
= (totalSecond / 60) % 60; int currentHour = (totalSecond / 60 / 60) % 24; int
timeZone = 0;// One hour in one time zone cout << "Enter the time zone offset to GMT:"; cin >>
timeZone; cout << "The current time is " << currentHour+timeZone << ":" <<
currentMinute << ":" << currentSecond; return 0; }
2.9( Physics : acceleration ) The average acceleration is defined as the change in rate divided by time , As shown in the following formula :a =
(v1-v0)/t, Programming , Prompt the user to enter the meter / The initial velocity in seconds v0, In meters / The final velocity in seconds v1, Time spent in seconds t, Output average acceleration .
#include <iostream> using namespace std; int main() { double v0 = 0, v1 = 0, t
= 0; cout << "Enter v0,v1 and t:"; cin >> v0 >> v1 >> t; double a = (v1 - v0) /
t; cout << "The average acceleration is " << a; return 0; }
2.10( Science and technology : Calculate the energy ) Programming , Calculate the energy required to heat the water from the initial temperature to the final temperature . The program prompts the user to input the amount of water in kilogram , Initial temperature and final temperature . The calculation formula is :Q
= M * (finalTemperature - initialTemperature)*
4184 among M Unit of water mass , The temperature is in degrees centigrade , energy Q It's in joules .
#include <iostream> using namespace std; int main() { const double RATE =
4184; double amountOfWater = 0; double initialTemperature = 0, finalTemperature
= 0; double energy = 0; cout << "Enter the amount of water in kilograms:"; cin
>> amountOfWater; cout << "Enter the initial temperature:"; cin >>
initialTemperature; cout << "Enter the final temperature:"; cin >>
finalTemperature; energy = amountOfWater * (finalTemperature -
initialTemperature)*RATE; cout << "The energy needed is " << energy << "J";
return 0; }
2.11( Population estimation ) Reprogramming exercises 1.11 Procedures , Prompt the user to enter the number of years , Output the population after so many years . Using programming exercises 1.11 Tips in .
#include <iostream> using namespace std; int main() { int numberOfYears = 0;
cout << "Enter the number of years:"; cin >> numberOfYears; int totalSecond =
numberOfYears * 365 * 24 * 60 * 60; cout << "The population in " <<
numberOfYears << " years is " << 312032486 + totalSecond / 7 + totalSecond / 45
- totalSecond / 13; return 0; }
2.12( Physics : Calculation of runway length ) Give the acceleration of the aircraft a And take-off speed v, The following formula can be used to calculate the shortest runway length required for aircraft take-off :length =
/2a, Programming , User input in meters / second (m/s) Speed in units of v And in meters / The square of a second (m/) Acceleration in units of a, Output the shortest runway length .
#include <iostream> #include <cmath> using namespace std; int main() { double
v = 0,a = 0, length = 0; cout << "Enter speed and acceleration:"; cin >> v >>
a; length = pow(v, 2) / 2 / a; cout << "The minimum runway length for this
airplance is " << length<<"m"; return 0; }
2.13( Financial application : Calculating the integer value of zero deposit ) Suppose that the user deposits money into a savings account every month 100 dollar , The annual interest rate is 5%. that , The monthly interest rate is 0.05 / 12 =
0.00417. After the first month , The carrying amount becomes
100 * (1 + 0.00417) = 100.417
After the second month , The carrying amount becomes
(100 + 100.417) * (1 + 0.00417) = 201.252
After the third month , The carrying amount becomes
(100 + 201.252) * (1 + 0.00417) = 302.507
and so on .
Programming , Prompt the user to input the amount of monthly savings , output 6 Book amount after three months .( Practice in programming 5.32 in , Will use the loop to simplify the code , And it is expanded to the book amount after any month .)
#include <iostream> using namespace std; int main() { const double YEARRATE =
0.05; double monthRate = YEARRATE / 12; double monthlySavingAmount = 0; cout <<
"Enter the monthly saving amount:"; cin >> monthlySavingAmount; double
accountValue = 0; accountValue = monthlySavingAmount * (1 + monthRate);//first
month accountValue = (monthlySavingAmount + accountValue)*(1 +
monthRate);// The second month accountValue = (monthlySavingAmount + accountValue)*(1 +
monthRate);// The third month accountValue = (monthlySavingAmount + accountValue)*(1 +
monthRate);// The fourth month accountValue = (monthlySavingAmount + accountValue)*(1 +
monthRate);// The fifth month accountValue = (monthlySavingAmount + accountValue)*(1 +
monthRate);// The sixth month cout << "After the sixth month, the account value is $" <<
accountValue; return 0; }
2.14( Health applications :BMI) Body mass index (BMI) Measuring health in terms of weight . It is calculated by dividing the weight in kilograms by the square of the height in meters . Programming , Prompt the user to enter the weight in pounds and the height in feet , output BMI. be careful ,1 Pound equals 0.45359237 Kilogram ,1 Feet 0.0254 rice .
#include <iostream> using namespace std; int main() { const double
KILOGRAMRATE = 0.45359237, METERRATE = 0.0254; double pound = 0, inche = 0;
cout << "Enter weight in pounds:"; cin >> pound; cout << "Enter height in
inches:"; cin >> inche; double BMI = 0.0; BMI = pound * KILOGRAMRATE /
(inche*METERRATE) / (inche*METERRATE); cout << "BMI is " << BMI; return 0; }
2.15( geometry : The distance between two points ) Programming , Prompt the user to enter two points (x1,y1),(x2,y2), Output the distance between them . The formula for calculating the distance is
Note that it can be used pow(a,0.5) To calculate .
#include <iostream> #include <cmath> using namespace std; int main() { double
x1 = 0, x2 = 0, y1 = 0, y2 = 0; cout << "Enter x1 and y1:"; cin >> x1 >> y1;
cout << "Enter x2 and y2:"; cin >> x2 >> y2; double distance = 0; distance =
pow(pow(x2 - x1, 2) + pow(y2 - y1, 2), 0.5); cout << "The distance between the
two points is " << distance; return 0; }
2.16( geometry : Area of hexagon ) Programming , Prompts the user to enter the edge of the hexagon , Output its area . The formula for calculating the area of a hexagon is Area = . among s It's the length of the side .
#include <iostream> #include <cmath> using namespace std; int main() { double
side = 0; cout << "Enter the side:"; cin >> side; double area =
1.5*sqrt(3.0)*side*side; cout << "The atea of the hexagon is " << area; return
0; }
2.17( Science and technology : Air cooling temperature ) How cold is it out there ? Temperature alone is not enough to provide the answer . Other factors include wind speed , Relative humidity and light play an important role in determining the degree of outdoor cold .2001 year , International climate Association (NWS) Fulfilled the new air cooling temperature , Use temperature and wind speed to measure coldness , The formula is :
Where is the outdoor temperature in degrees Fahrenheit ,v It's in miles per hour (mph) Speed in units of .
Is the air cooling temperature . When the wind speed is lower than 2mph Or the temperature is lower than -58 Degrees Fahrenheit or above 41 It can't be used in degrees Fahrenheit .
Programming , Prompt the user to enter the -58 Fahrenheit and 41 Temperatures between degrees Fahrenheit , Greater than or equal to 2mph Wind speed , Output air cooling temperature . use pow(a,b) To calculate .
#include <iostream> #include <cmath> using namespace std; int main() { double
twc = 0, ta = 0, v = 0; cout << "Enter the temperature in Fahrenheit:"; cin >>
ta; cout << "Enter the wind speed in miles per hour:"; cin >> v; twc = 35.74 +
0.6215*ta - 35.75*pow(v, 0.16) + 0.4275*ta*pow(v, 0.16); cout << "The wind
chill index is " << twc; return 0; }
2.18( Output a table ) Programming , Output the list below :
abpow(a,b)
121
238
3481
451024
5615625 #include<iostream> #include <cmath> using namespace std; int main() {
int a = 1, b = 2; cout << "a" << " " << "b" << " " << "pow(a,b)" << endl; cout
<< a << " " << b << " " << pow(a, b) << endl; a++, b++; cout << a << " " << b
<< " " << pow(a, b) << endl; a++, b++; cout << a << " " << b << " " << pow(a,
b) << endl; a++, b++; cout << a << " " << b << " " << pow(a, b) << endl; a++,
b++; cout << a << " " << b << " " << pow(a, b) << endl; return 0; }
2.19( geometry : Area of triangle ) Programming , Prompts the user to enter three vertices of the triangle (x1,y1),(x2,y2),(x3,y3), Output its area . The formula for calculating triangles is
s = (side1 + side3 + side3)/2
area =
#include <iostream> #include <cmath> using namespace std; int main() { double
x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0; cout << "Enter three points for
a triangle;"; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; double side1 =
pow(pow(x1 - x2, 2) + pow(y1 - y2, 2), 0.5); double side2 = pow(pow(x1 - x3, 2)
+ pow(y1 - y3, 2), 0.5); double side3 = pow(pow(x3 - x2, 2) + pow(y3 - y2, 2),
0.5); double s = (side1 + side2 + side3) / 2; double area = pow(s * (s - side1)
* (s - side2) * (s - side3), 0.5); cout << "The area of the triangle is " <<
area; return 0; }
2.20( The slope of the line ) Programming , Prompt the user to enter the coordinates of two points (x1,y1) and (x2,y2), Outputs the slope of the line connecting two points . The slope formula is (y2-y1)/(x2-x1).
#include <iostream> using namespace std; int main() { double x1 = 0, x2 = 0,
y1 = 0, y2 = 0; cout << "Enter the coordinates for two points:"; cin >> x1 >>
y1 >> x2 >> y2; double k = (y2 - y1) / (x2 - x1); cout << "The slope for the
line that connects two points " << "(" << x1 << "," << y1 << ") and (" << x2 <<
"," << y2 << ") is " << k; return 0; }
2.21( Driving expenses ) Programming , Prompt the user to enter the driving distance , Vehicle fuel efficiency in kilometers per gallon , Price per gallon , Export the cost of the trip .
#include <iostream> using namespace std; int main() { double distance = 0,
milesPerGallon = 0, pricePerGallon = 0; cout << "Eter the driving distance:";
cin >> distance; cout << "Enter miles per gallon:"; cin >> milesPerGallon; cout
<< "Enter price per gallon:"; cin >> pricePerGallon; double costOfDriving =
distance / milesPerGallon * pricePerGallon; costOfDriving =
((int)(costOfDriving * 100)) / 100.0;// Keep the result to two decimal places , That's to the nearest cent cout << "The cost of
driving is $" << costOfDriving; return 0; }
2.22( Financial application : Calculate interest ) Known balance and annual interest rate , The interest payment for next month can be calculated , Use the following formula :interest = balance *
(annualIntersRate / 1200). Programming , Read balance and annual interest rate , Output next month's interest .
#include <iostream> using namespace std; int main() { double interest = 0,
balance = 0, annualInterestRate = 0; cout << "Enter balance and interest rate
(e.g.3 for 3%):"; cin >> balance >> annualInterestRate; interest = balance *
(annualInterestRate / 1200); cout << "The interest is " << interest; return 0; }
2.23( Financial application : Future investment value ) Programming , Read in the amount of investment , Annual interest rate , number of years , Export future investment value , Use the following formula :
#include <iostream> #include <cmath> using namespace std; int main() { double
investmentAmount = 0, monthlyInterestRate = 0, numberOfYears = 0; double
annualInterestRate = 0; cout << "Enter investment amount:"; cin >>
investmentAmount; cout << "Enter annual interest rate in percentage:"; cin >>
annualInterestRate; cout << "Enter number of years:"; cin >> numberOfYears;
monthlyInterestRate = annualInterestRate / 1200;// Calculate monthly interest rate double
futureInvestmentValue = 0; futureInvestmentValue = investmentAmount * pow(1 +
monthlyInterestRate, numberOfYears * 12); cout << "Accumulated value is $" <<
futureInvestmentValue; return 0; }
2.24( Financial application : Monetary unit ) Rewrite program list 2-12,ComputeChange.cpp, Fix will be a float Value to int The loss of precision that may occur when the value of . The last two digits represent the whole number of cents . For example, input 1156 representative 11 Dollars and 56 cent .
#include <iostream> using namespace std; int main() { unsigned amount = 0;
cout << "Enter an amount in int,for example 1156 is 11 dollars and 56
pennies:"; cin >> amount; unsigned remainingAmount = amount; unsigned
numberOfOneDollars = remainingAmount / 100;// One dollar quantity remainingAmount =
remainingAmount % 100; unsigned numberOfQuarters = remainingAmount /
25;// Quarter quantity remainingAmount = remainingAmount % 25; unsigned numberOfDimes =
remainingAmount / 10;// Number of dimes remainingAmount %= 10; unsigned numberOfNickels =
remainingAmount / 5;// Five dollar mark remainingAmount %= 5; unsigned numberOfPennies =
remainingAmount;// One dollar fraction cout << "Your amount " << amount/100 <<" dollars and
"<<amount%100<<" pennies"<< " consists of " << endl << " " <<
numberOfOneDollars << " dollars" << endl << " " << numberOfQuarters << "
quarters" << endl << " " << numberOfDimes << " dimes" << endl << " " <<
numberOfNickels << " nickels" << endl << " " << numberOfPennies << " pennies"
<< endl; return 0; }
Technology