5  Arithmetic

5.0.1 Mathemagic with Arithmetic Operators

Ladies and Gentlemen, it’s time to pull out your magic wands - I mean, your keyboards - for our first act, the incredible, the unbelievable, the math-defying Arithmetic Operators!

Remember those characters you’ve met back in your Math class? Yes, the ones that liked adding apples and subtracting oranges! They’re here, and they’ve got their game face on. In Dart, they’ve got some cool tricks up their sleeves:

  • + The Addition Operator: This is like the super-friendly operator who’s always eager to bring things together. Have a couple of numbers? The addition operator will happily unite them for you.
int cats = 5;
int dogs = 3;
int totalPets = cats + dogs; // You have 8 lovely pets now.

- The Subtraction Operator: This operator is the detective of the group, always finding the difference. Feed it two numbers, and it’ll tell you how far apart they are.

int cakes = 10;
int eatenCakes = 3;
int remainingCakes = cakes - eatenCakes; // Still got 7 delicious cakes.

* The Multiplication Operator: Imagine you have a magic spell to duplicate things. That’s what the multiplication operator does, making your numbers multiply!

int hours = 24;
int days = 7;
int hoursInWeek = hours * days; // Abracadabra, there are 168 hours in a week.

/ The Division Operator: The peacekeeper of the group, the division operator evenly distributes one value among another, just like cutting a pie into equal pieces.

int pie = 10;
int friends = 2;
double piePerFriend = pie / friends; // Each friend gets 5.0 pieces of pie.

% The Modulus Operator: This sneaky operator is all about the leftovers. Divide one number by another, and the modulus will tell you what’s remaining.

int totalCandies = 11;
int numberofKids = 3;
int leftoverCandies = totalCandies % numberofKids; // 2 candies left after sharing among 3 kids.

Just like that, our mathemagicians are making wonders happen with your code. Now, it’s your turn. Grab those operators and start crafting your own spells. Remember, every great wizard starts with the basics!