Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.04 KB | None | 0 0
  1. copy this into pastebin, and then into github.
  2.  
  3. //2.16
  4. /*WRITE A PROGRAM THAT ASKS THE USER TO ENTER TWO NUMBERS, OBTAINS THE TWO NUMBERS FROM
  5. THE USER AND PRINTS THE SUM, PRODUCT, DIFFERENCE, AND QUOTIENT OF THE TWO NUMBERS. */
  6.  
  7. #include <iostream>
  8.  
  9. using std::cout;
  10. using std::cin;
  11. using std::endl;
  12.  
  13. int main()
  14. {
  15.     int number1{0};
  16.     //initializes the variable to zero.
  17.     int number2{0};
  18.     //initializes the variable to zero.
  19.  
  20.     cout << "Please enter two numbers." << endl;    
  21.     //Instructs the user to enter two numbers.
  22.     cout << "Number One: ";                        
  23.     //Tells user to enter number one.
  24.     cin >> number1;                        
  25.     //User's number1 input.
  26.     cout << "Number Two: ";                        
  27.     //Tells user to enter second number.
  28.     cin >> number2;                        
  29.     //User's number2 input.
  30.  
  31.     cout << "------ Results ------" << endl;        
  32.     //visual separator from input to output.
  33.     cout << "Sum: " << number1 + number2 << endl;  
  34.     //number sum.
  35.     cout << "Difference (Number One - Number Two): " << number1 - number2 << endl;
  36.     //number difference.
  37.     cout << "Product: " << number1 * number2 << endl;
  38.     //number product.
  39.     cout << "Quotient (Number One / Number Two: " << number1 / number2 << endl;
  40.     //number quotient.
  41.     cout << "------ Program Complete ------" << endl;
  42.     //tells user the program is over.
  43. }
  44.  
  45.  
  46.  
  47. //2.17
  48. /* WRITE A PROGRAM THAT PRINTS THE NUMBERS 1 TO 4
  49. ON THE SAME LINE WITH EACH PAIR OF ADJACENT NUMBERS
  50. SEPARATED BY ONE SPACE. DO THIS SEVERAL WAYS:
  51.  
  52. A) USING ONE STATEMENT WITH ONE STREAM INSERTION OPERATOR.
  53. B) USING ONE STATEMENT WITH FOUR STREAM INSERTION OPERATORS.
  54. C) USING FOUR STATEMENTS.*/
  55.  
  56. #include <iostream>
  57.  
  58. using namespace std;
  59. // Program uses names from the std namespace
  60.  
  61. int main()
  62. {
  63.     cout << "a) ";
  64.     cout << "1 2 3 4" << endl;
  65.     // A) uses one statement with one stream insertion operator.
  66.     cout << "b) ";
  67.     cout << "1 " << "2 " << "3 " << "4 " << endl;
  68.     // B) uses one statement but with four stream insertion operators.
  69.     cout << "c) ";
  70.     cout << "1 ";
  71.     cout << "2 ";
  72.     cout << "3 ";
  73.     cout << "4 " << endl;
  74.     // C) uses four separate statements.
  75. }
  76.  
  77.  
  78.  
  79. //2.18
  80. /* WRITE A PROGRAM THAT ASKS THE USER TO ENTER
  81. TWO INTEGERS, OBTAINS THE NUMBERS FROM THE USER,
  82. THEN PRINTS THE LARGER NUMBER FOLLOWED BY THE WORDS
  83. "IS LARGER." IF THE NUMBERS ARE EQUAL, PRINT THE MESSAGE
  84. "THESE NUMBERS ARE EQUAL."*/
  85.  
  86. #include <iostream>
  87.  
  88. using namespace std;
  89. // Program uses names from the std name space.
  90.  
  91. int main()
  92. {
  93.     int number1{0};
  94.     int number2{0};
  95.     // Declaring the variables used, and initializing them to zero.
  96.  
  97.     cout << "----- Which is larger...? -----" << endl;
  98.     //Displays a title for the program.
  99.     cout << "Please enter two numbers." << endl;
  100.     cout << "Number One: ";
  101.     cin >> number1;
  102.     //Prompts the user to input first number, stores value as number1.
  103.     cout << "Number Two: ";
  104.     cin >> number2;
  105.     //Prompts the user to input the second number, stores value as number2.
  106.  
  107.     cout << "----- RESULT -----" << endl;
  108.     //visual separator between inputs and output.
  109.  
  110.     if (number1 == number2) {
  111.  
  112.         cout << "These numbers are equal.";
  113.     }
  114.     // Condition: if the numbers are equal, the program will output a statement that  the numbers are equal.
  115.  
  116.     if (number1 > number2) {
  117.  
  118.         cout << number1 << " is larger.";
  119.     }
  120.     // Condition: if number1 is greater than number2, then the program will output a statement that number one is larger.
  121.  
  122.     if (number1 < number2) {
  123.  
  124.         cout << number2 << " is larger.";
  125.     }
  126.     // Condition: if number2 is greater than number1, then the program will output a statement that number two is larger.
  127.  
  128.  
  129. }
  130.  
  131.  
  132.  
  133. //2.19
  134. /* WRITE A PROGRAM THAT INPUTS THREE INTEGERS
  135. FROM THE KEYBOARD AND PRINTS THE SUM, AVERAGE,
  136. PRODUCT, SMALLEST AND LARGEST OF THESE NUMBERS.*/
  137.  
  138. #include <iostream>
  139.  
  140. using namespace std;
  141. // Program uses names from the std name space.
  142.  
  143. int main()
  144. {
  145.  
  146.     int number1{0};
  147.     int number2{0};
  148.     int number3{0};
  149.     //Defining the variables used and initializing them to zero.
  150.  
  151.     cout << "Input three different integers: ";
  152.     cin >> number1 >> number2 >> number3;
  153.     //Prompting the user to input three different integers.
  154.  
  155.     cout << "Sum is " << number1 + number2 + number3 << endl;
  156.     //Outputting the sum of the integers.
  157.  
  158.     cout << "Average is " << (number1 + number2 + number3) / 3 <<endl;
  159.     // Outputting the average of the integers.
  160.  
  161.     cout << "Product is " << number1 * number2 * number3 << endl;
  162.     // Outputting the product of the integers.
  163.  
  164.     // "SMALLEST IS" IF CONDITIONS:
  165.     if (number1 < number2 && number1 < number3 ) {
  166.  
  167.         cout << "Smallest is " << number1 << endl;
  168.     }
  169.  
  170.     if (number2 < number1 && number2 < number3) {
  171.  
  172.         cout << "Smallest is " << number2 << endl;
  173.     }
  174.  
  175.     if (number3 < number1 && number3 < number2){
  176.  
  177.         cout << "Smallest is " << number3 <<endl;
  178.     }
  179.  
  180.     //"LARGEST IS" IF CONDITIONS:
  181.     if (number1 > number2 && number1 > number3) {
  182.  
  183.         cout << "Largest is " << number1;
  184.     }
  185.  
  186.     if (number2 > number1 && number2 > number3) {
  187.  
  188.         cout << "Largest is " << number2;
  189.     }
  190.  
  191.     if (number3 > number1 && number3 > number2) {
  192.  
  193.         cout << "Largest is " << number3;
  194.     }
  195. }
  196.  
  197.  
  198.  
  199. //2.20
  200. /* WRITE A PROGRAM THAT READS IN THE RADIUS OF A CIRCLE AS
  201. AN INTEGER AND PRINTS THE CIRCLE'S DIAMETER, CIRCUMFERENCE
  202. AND AREA. USE THE CONSTANT VALUE 3.14159 FOR PI. DO ALL
  203. CALCULATIONS IN OUTPUT STATEMENTS.*/
  204.  
  205. #include <iostream>
  206.  
  207. using namespace std;
  208. // Program uses names from the std name space.
  209.  
  210. int main()
  211. {
  212.     double radius{0.0};
  213.     double pi{3.14159};
  214.     //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.
  215.  
  216.     cout << "----- CIRCLE CALCULATIONS -----" << endl;
  217.     //Program title... I like having titles displayed in my programs.
  218.     cout << "Enter the radius of circle as an integer:";
  219.     cin >> radius;
  220.     //Prompts user to input the radius of the circle.
  221.  
  222.     cout << "----- RESULTS -----" << endl;
  223.     //Result title.
  224.     cout << "Diameter: " << 2 * radius << endl;
  225.     // Outputs diameter.
  226.     cout << "Circumference: " << 2 * pi * radius << endl;
  227.     //Outputs circumference.
  228.     cout << "Area: " << pi * (radius * radius) << endl;
  229.     // Outputs area.
  230.  
  231. }
  232.  
  233.  
  234.  
  235. //Exercise 2.30: Making a Difference
  236. /* CREATE A BMI CALCULATOR APPLICATION THAT READS THE USER’S
  237. WEIGHT IN POUNDS AND HEIGHT IN INCHES (I DID BOTH POUNDS AND
  238. KILOGRAMS), THEN CALCULATES AND DISPLAYS THE USER’S BODY MASS INDEX. */
  239.  
  240. #include <iostream>
  241. #include <string>
  242.  
  243. int main()  {
  244.  
  245.     double userWeight{0.0};                                                                        
  246.     //defining variables and initializing them to zero.
  247.     double userHeight{0.0};                                                                        
  248.        //I am using double integers so that users can enter EXACT weights and receive exact BMI.
  249.     double bmiKM{0.0};
  250.     double bmiPF{0.0};
  251.     double bmi{0.0};
  252.  
  253.     std::cout << "----- BMI Calculator -----" << std::endl;
  254.     std::cout << "Please select units: Pounds and Feet (PF) or Kilograms and Meters (KM)";
  255.     std::string userUnit;
  256.     std::cin >> userUnit;
  257.  
  258.     if (userUnit == "PF") {
  259.         std::cout << "Enter your exact weight in Pounds: ";
  260.         std::cin >> userWeight;
  261.  
  262.         std::cout << "Enter your exact height in Feet: ";
  263.         std::cin >> userHeight;
  264.  
  265.         bmiPF = (userWeight * 703.0) / ((userHeight * 12.0) * (userHeight * 12.0));
  266.         std::cout << "-------------------------------" << std::endl;
  267.         std::cout << "Your Body Mass Index: " << bmiPF << std::endl;
  268.  
  269.         if (bmiPF < 18.5) {
  270.         std::cout << "You are underweight." << std::endl;                                          
  271.             //when BMI is underweight
  272.     }
  273.  
  274.     if (bmiPF <= 24.9 && bmiPF >= 18.5) {                                                          
  275.             //when BMI is average
  276.            //for or:  ||
  277.         std::cout << "You are average." << std::endl;
  278.     }
  279.  
  280.     if (bmiPF <= 29.9 && bmiPF >= 25.0) {                                                          
  281.             //when BMI is overweight
  282.         std::cout << "You are overweight." << std::endl;
  283.     }
  284.  
  285.     if (bmiPF >= 30.0 ) {                                                                          
  286.             //when BMI is obese.
  287.         std::cout << "You are obese." << std::endl;
  288.     }
  289.     }
  290.  
  291.     if (userUnit == "KM"){                                                                          
  292.             //IN ORDER TO GIVE THE USER MORE CHOICES, the user has a choice between measuring in kilos and pounds.
  293.             //This is the kilos option.
  294.         std::cout << "----- BMI Calculator -----" << std::endl;
  295.         std::cout << "Enter your exact weight in Kilograms: ";                                      
  296.             //User enters weight (Kilos).
  297.         std::cin >> userWeight;
  298.  
  299.         std::cout << "Enter your exact height in Meters: ";                                        
  300.             //User enters height (Meters).
  301.         std::cin >> userHeight;
  302.  
  303.         bmiKM = userWeight / (userHeight * userHeight);                                            
  304.             //This is the BMI formula.
  305.         std::cout << "-------------------------------" << std::endl;                                
  306.             //This is just to separate the results from the entered height and weight.
  307.         std::cout << "Your Body Mass Index: " << bmiKM <<std::endl;
  308.  
  309.     if (bmiKM < 18.5) {                                                                            
  310.             //when BMI is underweight
  311.         std::cout << "You are underweight." << std::endl;
  312.     }
  313.  
  314.  
  315.     if (bmiKM <= 24.9 && bmiKM >= 18.5) {                                                          
  316.             //when BMI is average
  317.            //for or:  ||
  318.        
  319.         std::cout << "You are average." << std::endl;
  320.     }
  321.  
  322.     if (bmiKM <= 29.9 && bmiKM >= 25.0) {                                                          
  323.             //when BMI is overweight
  324.         std::cout << "You are overweight." << std::endl;
  325.     }
  326.  
  327.     if (bmiKM >= 30.0 ) {                                                                          
  328.             //when BMI is obese.
  329.         std::cout << "You are obese." << std::endl;
  330.     }
  331.     }
  332.  
  333.     std::cout << "-------------------------------" << std::endl;                                    
  334.             //This is only for separating the results from the BMI values.
  335.  
  336.     std::cout << "----- BMI VALUES -----" << std::endl;
  337.     std::cout << "UNDERWEIGHT: less than 18.5" << std::endl;
  338.     std::cout << "AVERAGE: between 18.5 and 24.9" << std::endl;
  339.     std::cout << "OVERWEIGHT: between 25 and 29.9" << std::endl;
  340.     std::cout << "OBESE: 30 or greater" << std::endl;
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement