
Dart basic programs mostly asked in interviews
A variety of basic programs can help you learn the fundamentals of Dart programming. Common exercises include the classic “Hello, World!”, a simple calculator, and the FizzBuzz game. These examples cover essential concepts like variables, functions, and control flow.Â
1. Hello, World!
This is the standard first program for any new language. It demonstrates the basic structure of a Dart application.
dart
void main() {
print('Hello, World!');
}
Use code with caution.
Explanation:
void main()
: The entry point for any Dart program. The code inside the curly braces will be executed when the program runs.print('Hello, World!');
: A function that prints its string argument to the console.Â
2. Simple calculator
This command-line program takes two numbers and an operator as input and performs the corresponding calculation. It teaches user input, conditional statements, and type conversion.
dart
import 'dart:io';
void main() {
print("Enter the first number:");
var num1 = double.parse(stdin.readLineSync()!);
print("Enter the operator (+, -, *, /):");
var operator = stdin.readLineSync();
print("Enter the second number:");
var num2 = double.parse(stdin.readLineSync()!);
double result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
print("Invalid operator");
return;
}
print("Result: $result");
}
Use code with caution.
Explanation:
import 'dart:io'
: Imports the library needed for input/output operations.stdin.readLineSync()
: Reads a line of text entered by the user.double.parse()
: Converts the input string into a number with a decimal point.switch (operator)
: Checks the value of theÂoperator
 variable to decide which action to perform.Â
3. FizzBuzz
This program iterates from 1 to 100, printing “Fizz,” “Buzz,” or “FizzBuzz” based on divisibility rules. It is a classic coding challenge that demonstrates the use of loops and conditional logic.
dart
void main() {
for (var i = 1; i <= 100; i++) {
if (i % 3 == 0 && i % 5 == 0) {
print('FizzBuzz');
} else if (i % 3 == 0) {
print('Fizz');
} else if (i % 5 == 0) {
print('Buzz');
} else {
print(i);
}
}
}
Use code with caution.
Explanation:
for (var i = 1; i <= 100; i++)
: Creates a loop that runs 100 times.%
: The modulus operator, which returns the remainder of a division.if
/else if
/else
: Evaluates conditions to determine the correct output.Â
4. Reverse a string
This program takes a string and prints its characters in reverse order. It introduces you to strings and loops.
dart
import 'dart:io';
void main() {
stdout.write("Enter a string to reverse: ");
String? original = stdin.readLineSync();
if (original != null && original.isNotEmpty) {
String reversed = original.split('').reversed.join('');
print("Reversed string: $reversed");
} else {
print("Please enter a valid string.");
}
}
Use code with caution.
Explanation:
original.split('')
: Converts the string into a list of characters..reversed
: Reverses the order of the items in the list..join('')
: Joins the reversed list of characters back into a single string.if (original != null && original.isNotEmpty)
: Ensures the program doesn’t try to reverse a null or empty string.Â
5. Calculate simple interest
This program uses a function to calculate simple interest based on the principal amount, time, and rate. It is an excellent example of defining and using functions in Dart.
dart
void calculateInterest(double principal, double rate, double time) {
double interest = (principal * rate * time) / 100;
print("The simple interest is $interest");
}
void main() {
double principal = 1000.0;
double rate = 5.0;
double time = 2.0;
calculateInterest(principal, rate, time);
}
Use code with caution.
Explanation:
void calculateInterest(...)
: Defines a function that takes three arguments.(principal * rate * time) / 100
: The formula for calculating simple interest.calculateInterest(...)
: Calls the function from theÂmain
 function.Â
How to run these programs
- Install the Dart SDK: Follow the installation instructions on the official Dart website.
- Save the code: Save the code into a file with aÂ
.dart
 extension (e.g.,Âhello_world.dart
). - Run from the terminal: Open your terminal or command prompt, navigate to the file’s directory, and run the program with the commandÂ
dart file_name.dart
.Â
Pingback: How to use row and column in flutter - Dheeraj Hitech