Introduction
Dart is a modern, open-source programming language developed by Google, known for its use in developing mobile and web applications with the Flutter framework. Learning Dart involves understanding basic operations like arithmetic calculations. In this example, we will write a simple Dart program to subtract five numbers.
Dart Program to Subtract Five Numbers
Here’s a basic Dart program to subtract five numbers:
void main() {
// Define the five numbers
int num1 = 100;
int num2 = 20;
int num3 = 15;
int num4 = 10;
int num5 = 5;
// Perform the subtraction
int result = num1 - num2 - num3 - num4 - num5;
// Print the result
print('The result of subtracting the five numbers is: $result');
}
DartExplanation of the Code
void main()
: This is the entry point of the Dart program. Themain
function is where the execution of the program begins.int num1 = 100;
: Here, we define an integer variablenum1
and initialize it with the value100
.int num2 = 20;
: Similarly, we define another integer variablenum2
and initialize it with the value20
.int num3 = 15;
: This line defines the third integer variablenum3
with the value15
.int num4 = 10;
: This line defines the fourth integer variablenum4
with the value10
.int num5 = 5;
: This line defines the fifth integer variablenum5
with the value5
.int result = num1 - num2 - num3 - num4 - num5;
: We perform the subtraction of all five numbers and store the result in the variableresult
.print('The result of subtracting the five numbers is: $result');
: Finally, we use theprint
function to display the result of the subtraction on the console. The$result
is used to insert the value ofresult
into the string.
Output
When you run the program, the output will be:
The result of subtracting the five numbers is: 50
DartConclusion
This simple Dart program demonstrates basic arithmetic operations, specifically subtraction. By defining integer variables and performing subtraction, we can easily handle mathematical tasks in Dart. This foundational knowledge is essential as you progress to more complex programming concepts and applications. Dart’s straightforward syntax and powerful features make it a great choice for various programming tasks.