C++ Lab: 1A

Mon Jun 23 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Introduction

In this lab you will practice using variables and constants of type int and double, using integral and floating point expressions, and outputting variables to the display. Be sure to read this lab thoroughly, especially the Hand-in Procedure.

Lab

Write a complete program that:

  1. Calculates the value of the following formulas and writes the value out to the terminal along with an informative message. The formulas to calculate the values of are:
FORMULADATA TYPES
5 + 2int
5 - 2int
5 * 2int
5 / 2int
5 % 2int
5.0 / 2.0double

Use the data type specified with the values 5, 2, 5.0, 2.0 as defined constants. The result of the expression should be saved in an appropriate variable which is then output to the terminal along with the informative message.

See the example output for examples and formatting.

Example Output

An example of an interaction with your program is shown below, your output should match these examples exactly.

Alexs-iMac:lab1a alex$ g++ -Wall -pedantic -Werror main.cpp Alexs-iMac:lab1a alex$ ./a.out 5 + 2 = 7 5 - 2 = 3 5 * 2 = 10 5 / 2 = 2 5 % 2 = 1 5.0 / 2.0 = 2.5

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.