Dart Puzzles 101

Jesutoni Aderibigbe
3 min readOct 9, 2023

--

In my Track Meeting yesterday, I figured the need to re-emphasize the need to have a core understanding of the dart programming language in your basic understanding of Flutter.

Flutter is not a programming language. Thus, the mistake some people make in their use of Flutter is to jump into the framework without having a core understanding of Dart. Flutter is a framework that is written with Dart.

Therefore, every week, alongside my post on Flutter, I would also be dropping dart puzzles on different concepts.

LEZZ GO!

  1. Write a dart program that calculates the area of a rectangle. Prompt the user to enter the length and width of the rectangle, and then calculate and display the area.
void main(){
double length, width;

// Prompt the user to enter the length and width
stdout.write("Enter the length of the rectangle: ");
length = double.parse(stdin.readLineSync());

stdout.write("Enter the width of the rectangle: ");
width = double.parse(stdin.readLineSync());

// Calculate the area of the rectangle
double area = length * width;

// Display the result
print("The area of the rectangle with length $length and width $width is: $area");


}

2. Create a Dart program that asks the user for their age and stores it as an integer. Then, calculate their age in months (assume 30.44 days per month) and display the result as a double.

void main(){
stdout.write("Enter your age: ");
var input = stdin.readLineSync();
int age = int.parse(input);

double ageinMonths = age * 30.44
print("Your age in months is: $ageInMonths");

}

3. Write a Dart program that initializes a list of integers with some values. Calculate and print the sum of all the elements in the list. Use a for loop to iterate through the list.

List<int> numbers = [10, 20, 30, 40, 50]; // Initialize a list of integers
int sum = 0; // Initialize a variable to store the sum

// Iterate through the list using a for loop
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i]; // Add the current element to the sum
}

print("Sum of all elements in the list: $sum");

In this question, I simply created a list with random values. I stated that the sum of our numbers would start from 0. I used a for loop to iterate through the numbers list, adding each element to the sum variable.

4. Write a Dart program that initializes a list of integers. Then, remove all even numbers from the list and display the modified list. Use list manipulation methods to achieve this.

void main() {
List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Initialize a list of integers

// Use the removeWhere method to remove even numbers from the list
numbers.removeWhere((number) => number % 2 == 0);

print("Modified list after removing even numbers: $numbers");
}

5. Write a Dart program that initializes a list of integers with some values and then doubles each element in the list. Finally, print the modified list.

void main(){
List<int> numbers = [1,2,3,4,5]


// Double each element in the list
for (int i = 0; i < numbers.length; i++) {
numbers[i] *= 2;
}


print(numbers); //print the modified list


}

6. You are building a simple calculator application in Dart. Write a Dart program that takes two numbers as input from the user and performs the following operations on them:

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division

Ensure that the program handles different variable types (integers and doubles) correctly and displays the results with the appropriate variable type. Also, include error handling for division by zero.

Your program should prompt the user to enter two numbers and then display the results of the four operations.

import 'dart:io';

void main() {
try {
stdout.write("Enter the first number: ");
var input1 = stdin.readLineSync();
var num1 = num.tryParse(input1);

stdout.write("Enter the second number: ");
var input2 = stdin.readLineSync();
var num2 = num.tryParse(input2);

if (num1 == null || num2 == null) {
print("Invalid input. Please enter valid numeric values.");
return;
}

print("\nResults:");
print("$num1 + $num2 = ${num1 + num2}");
print("$num1 - $num2 = ${num1 - num2}");
print("$num1 * $num2 = ${num1 * num2}");

if (num2 == 0) {
print("Division by zero is not allowed.");
} else {
print("$num1 / $num2 = ${num1 / num2}");
}
} catch (e) {
print("An error occurred: $e");
}
}

Catch you, next week!

--

--