Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Chapter 2 exercises
- //This program runs exercises 2.16 - 2.20, 2.28 and 2.30 in order
- #include <iostream>
- using namespace std;
- double num1{0.0}, num2{0.0}, num3{0.0};
- //exercise 2.16
- //This program performs basic arithmetic operations on two numbers
- int arithmetic(double num1, double num2){
- cout << "Exercise 2.16\n" "Please input two numbers to calculate: " << endl;
- cin >> num1 >> num2;
- cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
- cout << num1 << " x " << num2 << " = " << num1 * num2 << endl;
- cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
- cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
- }
- //Exercise 2.17
- //This program displays "1 2 3 4" in three different ways
- int displayNumbers(){
- cout << "Exercise 2.17\n" "1 2 3 4" << endl;
- cout << "1 " << "2 " << "3 " << "4" << endl;
- cout << "1 ";
- cout << "2 ";
- cout << "3 ";
- cout << "4" << endl;
- }
- //Exercise 2.18
- //This program accepts two numbers and shows the user
- //which is larger or if they are equal
- int largerOrEqual(double num1, double num2) {
- cout << "Exercise 2.18\n" "Please enter two numbers to compare:" << endl;
- cin >> num1 >> num2;
- if (num1 == num2){
- cout << "These numbers are equal" << endl;
- }
- if(num1 > num2){
- cout << num1 << " is larger";
- }
- if (num1 < num2){
- cout << num2 << " is larger";
- }
- }
- //Exercise 2.19
- //This program performs basic arithmetic, and comparison on three numbers
- int compareAndCalculate(double num1, double num2, double num3) {
- cout << "Exercise 2.19\n" "Input three different integers: " << endl;
- cin >> num1 >> num2 >> num3;
- cout << "Sum is " << num1 + num2 + num3 << endl;
- cout << "Average is " << (num1 + num2 + num3) / 3 << endl;
- cout << "Product is " << num1 * num2 * num3 << endl;
- if (num1 > num2 && num2 > num3){
- cout << "Largest is " << num1 << endl;
- cout << "Smallest is " << num3 << endl;
- }
- if (num1 > num3 && num3 > num2) {
- cout << "Largest is " << num1 << endl;
- cout << "Smallest is " << num2 << endl;
- }
- if (num2 > num1 && num1 > num3) {
- cout << "Largest is " << num2 << endl;
- cout << "Smallest is " << num3 << endl;
- }
- if (num2 > num3 && num3 > num1) {
- cout << "Largest is " << num2 << endl;
- cout << "Smallest is " << num1 << endl;
- }
- if (num3 > num2 && num2 > num1) {
- cout << "Largest is " << num3 << endl;
- cout << "Smallest is " << num1 << endl;
- }
- if (num3 > num1 && num1 > num2) {
- cout << "Largest is " << num3 << endl;
- cout << "Smallest is " << num2 << endl;
- }
- }
- //Exercise 2.20
- //This program accepts a radius of a circle
- //and prints the diameter, circumference, and area
- int circleDimensions() {
- int radius{};
- cout << "Exercise 2.20\n" "Please input the radius of your circle: " << endl;
- cin >> radius;
- cout << "Diameter is " << radius * 2 << endl;
- cout << "Circumference is " << 2 * 3.14159 * radius << endl;
- cout << "Area is " << 3.14159 * radius * radius << endl;
- }
- //Exercise 2.28
- //This program accepts a five digit number and prints that number with spaces
- int splitFiveDigits(int num1) {
- cout << "Exercise 2.28\n" "Please input a five digit number: " << endl;
- cin >> num1 ;
- cout << num1 / 10000 << " " << num1 % 10000 / 1000 << " "
- << num1 % 10000 % 1000 / 100 << " " << num1 % 10000 % 1000 % 100 / 10
- << " " << num1 % 10000 % 1000 % 100 % 10 << endl;
- }
- //Exercise 2.30
- //This program accepts weight and height, and calculates BMI
- int BMI(){
- double height{}, weight{};
- cout << "Exercise 2.30\n" "Please input your height in inches and weight in pounds: " << endl;
- cin >> height >> weight;
- cout << "Your BMI is " << (weight * 703) / (height * height) << endl;
- cout << "\n" "BMI VALUES\n"
- "Underweight:\t less than 18.5\n"
- "Normal:\t\t between 18.5 and 24.9\n"
- "Overweight:\t between 25 and 29.9\n"
- "Obese:\t\t 30 or greater" << endl;
- }
- int main() {
- arithmetic(num1, num2);
- cout << "\n";
- displayNumbers();
- cout << "\n";
- largerOrEqual(num1, num2);
- cout << "\n" << endl;
- compareAndCalculate(num1, num2, num3);
- cout << "\n";
- circleDimensions();
- cout << "\n";
- splitFiveDigits(num1);
- cout << "\n";
- BMI();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement