Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- copy this into pastebin, and then into github.
- //2.16
- /*WRITE A PROGRAM THAT ASKS THE USER TO ENTER TWO NUMBERS, OBTAINS THE TWO NUMBERS FROM
- THE USER AND PRINTS THE SUM, PRODUCT, DIFFERENCE, AND QUOTIENT OF THE TWO NUMBERS. */
- #include <iostream>
- using std::cout;
- using std::cin;
- using std::endl;
- int main()
- {
- int number1{0};
- //initializes the variable to zero.
- int number2{0};
- //initializes the variable to zero.
- cout << "Please enter two numbers." << endl;
- //Instructs the user to enter two numbers.
- cout << "Number One: ";
- //Tells user to enter number one.
- cin >> number1;
- //User's number1 input.
- cout << "Number Two: ";
- //Tells user to enter second number.
- cin >> number2;
- //User's number2 input.
- cout << "------ Results ------" << endl;
- //visual separator from input to output.
- cout << "Sum: " << number1 + number2 << endl;
- //number sum.
- cout << "Difference (Number One - Number Two): " << number1 - number2 << endl;
- //number difference.
- cout << "Product: " << number1 * number2 << endl;
- //number product.
- cout << "Quotient (Number One / Number Two: " << number1 / number2 << endl;
- //number quotient.
- cout << "------ Program Complete ------" << endl;
- //tells user the program is over.
- }
- //2.17
- /* WRITE A PROGRAM THAT PRINTS THE NUMBERS 1 TO 4
- ON THE SAME LINE WITH EACH PAIR OF ADJACENT NUMBERS
- SEPARATED BY ONE SPACE. DO THIS SEVERAL WAYS:
- A) USING ONE STATEMENT WITH ONE STREAM INSERTION OPERATOR.
- B) USING ONE STATEMENT WITH FOUR STREAM INSERTION OPERATORS.
- C) USING FOUR STATEMENTS.*/
- #include <iostream>
- using namespace std;
- // Program uses names from the std namespace
- int main()
- {
- cout << "a) ";
- cout << "1 2 3 4" << endl;
- // A) uses one statement with one stream insertion operator.
- cout << "b) ";
- cout << "1 " << "2 " << "3 " << "4 " << endl;
- // B) uses one statement but with four stream insertion operators.
- cout << "c) ";
- cout << "1 ";
- cout << "2 ";
- cout << "3 ";
- cout << "4 " << endl;
- // C) uses four separate statements.
- }
- //2.18
- /* WRITE A PROGRAM THAT ASKS THE USER TO ENTER
- TWO INTEGERS, OBTAINS THE NUMBERS FROM THE USER,
- THEN PRINTS THE LARGER NUMBER FOLLOWED BY THE WORDS
- "IS LARGER." IF THE NUMBERS ARE EQUAL, PRINT THE MESSAGE
- "THESE NUMBERS ARE EQUAL."*/
- #include <iostream>
- using namespace std;
- // Program uses names from the std name space.
- int main()
- {
- int number1{0};
- int number2{0};
- // Declaring the variables used, and initializing them to zero.
- cout << "----- Which is larger...? -----" << endl;
- //Displays a title for the program.
- cout << "Please enter two numbers." << endl;
- cout << "Number One: ";
- cin >> number1;
- //Prompts the user to input first number, stores value as number1.
- cout << "Number Two: ";
- cin >> number2;
- //Prompts the user to input the second number, stores value as number2.
- cout << "----- RESULT -----" << endl;
- //visual separator between inputs and output.
- if (number1 == number2) {
- cout << "These numbers are equal.";
- }
- // Condition: if the numbers are equal, the program will output a statement that the numbers are equal.
- if (number1 > number2) {
- cout << number1 << " is larger.";
- }
- // Condition: if number1 is greater than number2, then the program will output a statement that number one is larger.
- if (number1 < number2) {
- cout << number2 << " is larger.";
- }
- // Condition: if number2 is greater than number1, then the program will output a statement that number two is larger.
- }
- //2.19
- /* WRITE A PROGRAM THAT INPUTS THREE INTEGERS
- FROM THE KEYBOARD AND PRINTS THE SUM, AVERAGE,
- PRODUCT, SMALLEST AND LARGEST OF THESE NUMBERS.*/
- #include <iostream>
- using namespace std;
- // Program uses names from the std name space.
- int main()
- {
- int number1{0};
- int number2{0};
- int number3{0};
- //Defining the variables used and initializing them to zero.
- cout << "Input three different integers: ";
- cin >> number1 >> number2 >> number3;
- //Prompting the user to input three different integers.
- cout << "Sum is " << number1 + number2 + number3 << endl;
- //Outputting the sum of the integers.
- cout << "Average is " << (number1 + number2 + number3) / 3 <<endl;
- // Outputting the average of the integers.
- cout << "Product is " << number1 * number2 * number3 << endl;
- // Outputting the product of the integers.
- // "SMALLEST IS" IF CONDITIONS:
- if (number1 < number2 && number1 < number3 ) {
- cout << "Smallest is " << number1 << endl;
- }
- if (number2 < number1 && number2 < number3) {
- cout << "Smallest is " << number2 << endl;
- }
- if (number3 < number1 && number3 < number2){
- cout << "Smallest is " << number3 <<endl;
- }
- //"LARGEST IS" IF CONDITIONS:
- if (number1 > number2 && number1 > number3) {
- cout << "Largest is " << number1;
- }
- if (number2 > number1 && number2 > number3) {
- cout << "Largest is " << number2;
- }
- if (number3 > number1 && number3 > number2) {
- cout << "Largest is " << number3;
- }
- }
- //2.20
- /* WRITE A PROGRAM THAT READS IN THE RADIUS OF A CIRCLE AS
- AN INTEGER AND PRINTS THE CIRCLE'S DIAMETER, CIRCUMFERENCE
- AND AREA. USE THE CONSTANT VALUE 3.14159 FOR PI. DO ALL
- CALCULATIONS IN OUTPUT STATEMENTS.*/
- #include <iostream>
- using namespace std;
- // Program uses names from the std name space.
- int main()
- {
- double radius{0.0};
- double pi{3.14159};
- //defining variables and setting the input-type variables to zero; since pi is going to stay the same value, pi is equal to its permanent value.
- cout << "----- CIRCLE CALCULATIONS -----" << endl;
- //Program title... I like having titles displayed in my programs.
- cout << "Enter the radius of circle as an integer:";
- cin >> radius;
- //Prompts user to input the radius of the circle.
- cout << "----- RESULTS -----" << endl;
- //Result title.
- cout << "Diameter: " << 2 * radius << endl;
- // Outputs diameter.
- cout << "Circumference: " << 2 * pi * radius << endl;
- //Outputs circumference.
- cout << "Area: " << pi * (radius * radius) << endl;
- // Outputs area.
- }
- //Exercise 2.30: Making a Difference
- /* CREATE A BMI CALCULATOR APPLICATION THAT READS THE USER’S
- WEIGHT IN POUNDS AND HEIGHT IN INCHES (I DID BOTH POUNDS AND
- KILOGRAMS), THEN CALCULATES AND DISPLAYS THE USER’S BODY MASS INDEX. */
- #include <iostream>
- #include <string>
- int main() {
- double userWeight{0.0};
- //defining variables and initializing them to zero.
- double userHeight{0.0};
- //I am using double integers so that users can enter EXACT weights and receive exact BMI.
- double bmiKM{0.0};
- double bmiPF{0.0};
- double bmi{0.0};
- std::cout << "----- BMI Calculator -----" << std::endl;
- std::cout << "Please select units: Pounds and Feet (PF) or Kilograms and Meters (KM)";
- std::string userUnit;
- std::cin >> userUnit;
- if (userUnit == "PF") {
- std::cout << "Enter your exact weight in Pounds: ";
- std::cin >> userWeight;
- std::cout << "Enter your exact height in Feet: ";
- std::cin >> userHeight;
- bmiPF = (userWeight * 703.0) / ((userHeight * 12.0) * (userHeight * 12.0));
- std::cout << "-------------------------------" << std::endl;
- std::cout << "Your Body Mass Index: " << bmiPF << std::endl;
- if (bmiPF < 18.5) {
- std::cout << "You are underweight." << std::endl;
- //when BMI is underweight
- }
- if (bmiPF <= 24.9 && bmiPF >= 18.5) {
- //when BMI is average
- //for or: ||
- std::cout << "You are average." << std::endl;
- }
- if (bmiPF <= 29.9 && bmiPF >= 25.0) {
- //when BMI is overweight
- std::cout << "You are overweight." << std::endl;
- }
- if (bmiPF >= 30.0 ) {
- //when BMI is obese.
- std::cout << "You are obese." << std::endl;
- }
- }
- if (userUnit == "KM"){
- //IN ORDER TO GIVE THE USER MORE CHOICES, the user has a choice between measuring in kilos and pounds.
- //This is the kilos option.
- std::cout << "----- BMI Calculator -----" << std::endl;
- std::cout << "Enter your exact weight in Kilograms: ";
- //User enters weight (Kilos).
- std::cin >> userWeight;
- std::cout << "Enter your exact height in Meters: ";
- //User enters height (Meters).
- std::cin >> userHeight;
- bmiKM = userWeight / (userHeight * userHeight);
- //This is the BMI formula.
- std::cout << "-------------------------------" << std::endl;
- //This is just to separate the results from the entered height and weight.
- std::cout << "Your Body Mass Index: " << bmiKM <<std::endl;
- if (bmiKM < 18.5) {
- //when BMI is underweight
- std::cout << "You are underweight." << std::endl;
- }
- if (bmiKM <= 24.9 && bmiKM >= 18.5) {
- //when BMI is average
- //for or: ||
- std::cout << "You are average." << std::endl;
- }
- if (bmiKM <= 29.9 && bmiKM >= 25.0) {
- //when BMI is overweight
- std::cout << "You are overweight." << std::endl;
- }
- if (bmiKM >= 30.0 ) {
- //when BMI is obese.
- std::cout << "You are obese." << std::endl;
- }
- }
- std::cout << "-------------------------------" << std::endl;
- //This is only for separating the results from the BMI values.
- std::cout << "----- BMI VALUES -----" << std::endl;
- std::cout << "UNDERWEIGHT: less than 18.5" << std::endl;
- std::cout << "AVERAGE: between 18.5 and 24.9" << std::endl;
- std::cout << "OVERWEIGHT: between 25 and 29.9" << std::endl;
- std::cout << "OBESE: 30 or greater" << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement