Arithmetic Operators
Part of the Fundamentals section of Coddy's Dart journey — lesson 16 of 94.
Arithmetic operators in Dart perform mathematical calculations: addition (+), subtraction (-), multiplication (*), and division (/).
void main() {
int num1 = 10;
int num2 = 5;
int sum = num1 + num2;
print('Sum: $num1 + $num2 = $sum');
}void main() {
int num1 = 10;
int num2 = 5;
int difference = num1 - num2;
print('Difference: $num1 - $num2 = $difference');
}void main() {
int num1 = 10;
int num2 = 5;
int product = num1 * num2;
print('Product: $num1 * $num2 = $product');
}void main() {
int num1 = 10;
int num2 = 5;
double quotient = num1 / num2;
print('Quotient: $num1 / $num2 = $quotient');
}void main() {
int intValue = 5;
double doubleValue = 2.5;
double result = intValue + doubleValue;
print('$intValue + $doubleValue = $result');
}Challenge
BeginnerCreate a Dart program that performs basic arithmetic operations:
- Declare an integer variable named
num1with a value of 10 - Declare an integer variable named
num2with a value of 5 - Calculate and store the following operations in appropriately named variables:
- The sum of
num1andnum2(store insum) - The difference when
num2is subtracted fromnum1(store indifference) - The product of
num1andnum2(store inproduct) - The result when
num1is divided bynum2(store inquotientas a double) - Print each result with the EXACT following format:
Sum: 15
Difference: 5
Product: 50
Quotient: 2.0Your output must match this exact format.
Cheat sheet
Dart supports four basic arithmetic operators:
+for addition-for subtraction*for multiplication/for division
int num1 = 10;
int num2 = 5;
int sum = num1 + num2; // 15
int difference = num1 - num2; // 5
int product = num1 * num2; // 50
double quotient = num1 / num2; // 2.0You can mix int and double types in arithmetic operations:
int intValue = 5;
double doubleValue = 2.5;
double result = intValue + doubleValue; // 7.5Try it yourself
void main() {
// Declare your variables here
int num1 = ?;
int num2 = ?;
// Perform arithmetic operations
int sum = ?;
int difference = ?;
int product = ?;
double quotient = ?;
// Print the results
print("Sum: $sum");
print("Difference: $difference");
print("Product: $product");
print("Quotient: $quotient");
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Comparison OperatorsLogical ANDLogical ORLogical NOTType Test OperatorsRecap - Making Comparisons7Working with Strings
String ConcatenationString InterpolationMulti-line StringsString PropertiesBasic String Methods10Collections - Maps Basics
What are Maps?Creating a MapAccessing Values by KeyKey-Value PairsGetting Map SizeGetting KeysGetting ValuesChecking if a Key Exists13Null Safety In Depth
Understanding NullNullable TypesNon-Nullable TypesNull Assertion OperatorLate InitializationRecap - Handling Null Safely16Fundamentals Challenges
Challenge: List of calculationChallenge: Sum of numbersChallenge: Find product2Variables and Basic Data Types
What are Variables?StringsIntegers (int)Doubles (double)Booleans (bool)Type Inference with 'var'Final VariablesConstant VariablesNaming ConventionsBasic Null SafetyRecap - Declaring Variables8Control Flow - Loops
The 'for' LoopThe 'while' LoopThe 'do-while' LoopUsing 'break' in LoopsUsing 'continue' in LoopsRecap - Repeating Code3Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorIncrement and DecrementAssignment ShortcutsRecap - Simple Calculations6Control Flow - Decision Making
The 'if' StatementThe 'else' StatementThe 'else if' StatementRecap - Simple DecisionsNested 'if' StatementsThe 'switch' Statement9Collections - Lists Basics
What are Lists?Creating a ListAccessing by IndexGetting List LengthAdding ElementsRemoving ElementsChecking if a List is EmptyIterating Over a List12Functions Advanced
Optional Positional ParametersNamed ParametersRequired Named ParametersDefault Parameter ValuesRecap - Function Parameters15Project: Simple Calculator
Setting UpDeclaring Number