DSA SheetLesson · no judge

DSA FUNDAMENTALSMATH BASICS

Basic arithmetic uses operators, types, and operand order

Reading · 5 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 13

Each basic operation needs its own programming operator

Start with the unchanged values a = 17 and b = 5. Addition uses +, subtraction uses -, multiplication uses *, and division uses /. The four expressions below use the same pair of values, but each operator asks for a different calculation.

CPPThe four arithmetic operators applied to a = 17 and b = 5.
int a = 17;
int b = 5;

int addition = a + b;       // 22
int subtraction = a - b;    // 12
int multiplication = a * b; // 85
int division = a / b;       // 3

The multiplication operator is *, not x. The letter x is an ordinary variable name in code, so writing a x b is not multiplication syntax. The first three results match the familiar handwritten calculations directly. Division also produces a result, but the type of its operands determines whether that result can contain a fractional part.

CHECKPOINT 1Not answered

Which set of expressions correctly produces 22, 12, 85, and 3 from a = 17 and b = 5?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

The left operand determines the meaning of subtraction and division

Subtraction and division are ordered operations. In a - b, the left operand a is subtracted from nothing? No, b is subtracted from a, so 17 - 5 gives 12. In a / b, a is divided by b, so 17 / 5 gives the quotient for 17 divided by 5. Swapping the operands changes the expression because b - a and b / a ask different questions.

CPPChanging the operand order changes subtraction and division.
int a = 17;
int b = 5;

int firstDifference = a - b; // 12
int firstQuotient = a / b;    // 3

int reversedDifference = b - a; // -12
int reversedQuotient = b / a;    // 0

The expression a - b does not mean the same thing as b - a, and a / b does not mean the same thing as b / a. The original values remain 17 and 5 in every line above. Only their positions change, so each result still describes those two values, but it describes them in a different relationship.

Dividing two int values produces an int quotient

When both operands of a division expression are int values, C++ and Java perform integer division. For 17 / 5, the whole-number quotient is 3 and the fractional part .4 is discarded. This is not rounding: 3.4 becomes 3 because an int result cannot contain the fraction.

CPPConverting an operand before division allows a fractional result.
int a = 17;
int b = 5;

int whole = a / b;              // 3
 double exact = static_cast<double>(a) / b; // 3.4

To obtain 3.4, convert at least one operand to a floating-point type before the division takes place. The expression static_cast<double>(a) / b has a floating-point left operand, so the division keeps the fraction. In Java, writing (double) a / b has the same effect. Here b is 5, not zero, so the division is valid.

CHECKPOINT 2Not answered

Complete this expression so the division of 17 and 5 produces 3.4 instead of 3.

double result = __________;

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

Keeping both inputs unchanged makes all four results describe the same pair

Keep a = 17 and b = 5 unchanged, then calculate each result in its own expression. Separate names or labels make it clear which operation produced 22, 12, 85, and 3. The division result is an integer quotient because a and b are both int values.

CPPEach result is calculated from the same unchanged inputs.
int a = 17;
int b = 5;

int addition = a + b;
int subtraction = a - b;
int multiplication = a * b;
int division = a / b;

cout << "Addition: " << addition << endl;
cout << "Subtraction: " << subtraction << endl;
cout << "Multiplication: " << multiplication << endl;
cout << "Division: " << division << endl;

Replacing an input while calculating one result changes what later expressions mean. For example, if you assign a = a + b, then a is 22 instead of 17. A later expression a - b becomes 17, not 12, so it no longer describes the original pair 17 and 5. Calculate into separate result variables when every answer must use the original inputs.

Next part · Quiz