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.
This lab consists of the following tasks:
Prompting for and Reading User Input:
Performing Floating-Point Arithmetic:
Demonstrating Integer Division:
Displaying Results:
Create variables to store two user-provided floating-point numbers.
Use input/output statements to prompt the user and read their numbers.
Perform floating-point arithmetic (addition, subtraction, multiplication, division) and store the results.
Convert the floating-point inputs to integers.
Perform integer division and modulus operations using the integer values and store the results.
Display the results of all operations (floating-point and integer) with descriptive and formatted messages.
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.
Save: Save your code as main.cpp
. Do not ignore this step or save your file(s) with different names.
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.