Advertisement
Nofxthepirate

Chapter 2 Exercises

Jan 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1. //Chapter 2 exercises
  2. //This program runs exercises 2.16 - 2.20, 2.28 and 2.30 in order
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. double num1{0.0}, num2{0.0}, num3{0.0};
  7.  
  8. //exercise 2.16
  9. //This program performs basic arithmetic operations on two numbers
  10. int arithmetic(double num1, double num2){
  11.     cout << "Exercise 2.16\n" "Please input two numbers to calculate: " << endl;
  12.     cin >> num1 >> num2;
  13.     cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
  14.     cout << num1 << " x " << num2 << " = " << num1 * num2 << endl;
  15.     cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
  16.     cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
  17.  
  18. }
  19.  
  20. //Exercise 2.17
  21. //This program displays "1 2 3 4" in three different ways
  22. int displayNumbers(){
  23.     cout << "Exercise 2.17\n" "1 2 3 4" << endl;
  24.     cout << "1 " << "2 " << "3 " << "4" << endl;
  25.     cout << "1 ";
  26.     cout << "2 ";
  27.     cout << "3 ";
  28.     cout << "4" << endl;
  29. }
  30.  
  31. //Exercise 2.18
  32. //This program accepts two numbers and shows the user
  33. //which is larger or if they are equal
  34. int largerOrEqual(double num1, double num2) {
  35.     cout << "Exercise 2.18\n" "Please enter two numbers to compare:" << endl;
  36.     cin >> num1 >> num2;
  37.     if (num1 == num2){
  38.         cout << "These numbers are equal" << endl;
  39.     }
  40.     if(num1 > num2){
  41.         cout << num1 << " is larger";
  42.     }
  43.     if (num1 < num2){
  44.         cout << num2 << " is larger";
  45.     }
  46.  
  47. }
  48.  
  49. //Exercise 2.19
  50. //This program performs basic arithmetic, and comparison on three numbers
  51. int compareAndCalculate(double num1, double num2, double num3) {
  52.     cout << "Exercise 2.19\n" "Input three different integers: " << endl;
  53.     cin >> num1 >> num2 >> num3;
  54.     cout << "Sum is " << num1 + num2 + num3 << endl;
  55.     cout << "Average is " << (num1 + num2 + num3) / 3 << endl;
  56.     cout << "Product is " << num1 * num2 * num3 << endl;
  57.  
  58.     if (num1 > num2 && num2 > num3){
  59.         cout << "Largest is " << num1 << endl;
  60.         cout << "Smallest is " << num3 << endl;
  61.     }
  62.     if (num1 > num3 && num3 > num2) {
  63.         cout << "Largest is " << num1 << endl;
  64.         cout << "Smallest is " << num2 << endl;
  65.     }
  66.     if (num2 > num1 && num1 > num3) {
  67.         cout << "Largest is " << num2 << endl;
  68.         cout << "Smallest is " << num3 << endl;
  69.     }
  70.     if (num2 > num3 && num3 > num1) {
  71.         cout << "Largest is " << num2 << endl;
  72.         cout << "Smallest is " << num1 << endl;
  73.     }
  74.     if (num3 > num2 && num2 > num1) {
  75.         cout << "Largest is " << num3 << endl;
  76.         cout << "Smallest is " << num1 << endl;
  77.     }
  78.     if (num3 > num1 && num1 > num2) {
  79.         cout << "Largest is " << num3 << endl;
  80.         cout << "Smallest is " << num2 << endl;
  81.     }
  82. }
  83.  
  84. //Exercise 2.20
  85. //This program accepts a radius of a circle
  86. //and prints the diameter, circumference, and area
  87. int circleDimensions() {
  88.     int radius{};
  89.     cout << "Exercise 2.20\n" "Please input the radius of your circle: " << endl;
  90.     cin >> radius;
  91.     cout << "Diameter is " << radius * 2 << endl;
  92.     cout << "Circumference is " << 2 * 3.14159 * radius << endl;
  93.     cout << "Area is " << 3.14159 * radius * radius << endl;
  94.  
  95. }
  96.  
  97. //Exercise 2.28
  98. //This program accepts a five digit number and prints that number with spaces
  99. int splitFiveDigits(int num1) {
  100.     cout << "Exercise 2.28\n" "Please input a five digit number: " << endl;
  101.     cin >> num1 ;
  102.     cout << num1 / 10000 << " " << num1 % 10000 / 1000 << " "
  103.     << num1 % 10000 % 1000 / 100 << " " << num1 % 10000 % 1000 % 100 / 10
  104.     << " " << num1 % 10000 % 1000 % 100 % 10 << endl;
  105.  
  106. }
  107. //Exercise 2.30
  108. //This program accepts weight and height, and calculates BMI
  109. int BMI(){
  110.     double height{}, weight{};
  111.     cout << "Exercise 2.30\n" "Please input your height in inches and weight in pounds: " << endl;
  112.     cin >> height >> weight;
  113.     cout << "Your BMI is " << (weight * 703) / (height * height) << endl;
  114.     cout << "\n" "BMI VALUES\n"
  115.     "Underweight:\t less than 18.5\n"
  116.     "Normal:\t\t between 18.5 and 24.9\n"
  117.     "Overweight:\t between 25 and 29.9\n"
  118.     "Obese:\t\t 30 or greater" << endl;
  119. }
  120.  
  121. int main() {
  122.     arithmetic(num1, num2);
  123.     cout << "\n";
  124.     displayNumbers();
  125.     cout << "\n";
  126.     largerOrEqual(num1, num2);
  127.     cout << "\n" << endl;
  128.     compareAndCalculate(num1, num2, num3);
  129.     cout << "\n";
  130.     circleDimensions();
  131.     cout << "\n";
  132.     splitFiveDigits(num1);
  133.     cout << "\n";
  134.     BMI();
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement