Lab: Simple Arithmetic

Tue Jun 17 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Purpose

This lab introduces you to working with floating-point and integer data types by combining user input, arithmetic operations, and formatted output. You will write a program that takes two numbers as floating-point inputs, performs arithmetic operations (addition, subtraction, multiplication, division) using floating-point calculations, and then converts the inputs to integers to demonstrate integer division. This lab emphasizes the differences between floating-point and integer arithmetic, reinforcing key programming concepts that are fundamental for understanding data types and numerical operations in programming.


Task

This lab consists of the following tasks:

  1. Prompting for and Reading User Input:

    • Prompt the user to input two numbers as floating-point values (doubles).
    • Store the input values in appropriately typed variables.
  2. Performing Floating-Point Arithmetic:

    • Using the floating-point numbers provided by the user, perform addition, subtraction, multiplication, and division.
    • Store the results of these operations in variables.
  3. Demonstrating Integer Division:

    • Convert the floating-point inputs to integers.
    • Using the integer values, perform integer division and calculate the modulus.
    • Store the results in variables.
  4. Displaying Results:

    • Format and display the results of the floating-point operations with clear, descriptive messages.
    • Display the results of integer division and modulus with a message explaining the operation.

Steps to Complete the Lab

  1. Create variables to store two user-provided floating-point numbers.

  2. Use input/output statements to prompt the user and read their numbers.

  3. Perform floating-point arithmetic (addition, subtraction, multiplication, division) and store the results.

  4. Convert the floating-point inputs to integers.

  5. Perform integer division and modulus operations using the integer values and store the results.

  6. Display the results of all operations (floating-point and integer) with descriptive and formatted messages.


Criteria for Success

See CodeGrade

Example Program Interaction

Enter the first number (floating-point): 12.7 Enter the second number (floating-point): 3.4 Floating-point results: Sum: 16.1 Difference: 9.3 Product: 43.18 Quotient: 3.73529 Integer results (using truncated values): Integer Division Quotient: 4 Modulus: 0

See CodeGrade for more examples.

Note: The line breaks after the inputs in the example output formatting above are the ones placed into the terminal by the user hitting enter on their keyboard to input. CodeGrade does not enter values with a keyboard, but rather supplies input via Linux redirection to programs. So in CodeGrade's auto tests there will not be line breaks after the inputs.


Submission Instructions

  1. Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.

  2. Submit: Your program source code must be submitted via CodeGrade as a properly named .cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.