Sum of Two Numbers
To find the sum of two numbers, read the two numbers from user via standard input, pass the two numbers as operands to Addition Operator, and store the result in a variable.
Dart Program
In the following program, we read two numbers into x
and y
, find their sum, and store the result in output
.
main.dart
import 'dart:io'; void main(){ //read number from user print('Enter x'); var x = int.parse(stdin.readLineSync()!); print('Enter y'); var y = int.parse(stdin.readLineSync()!); var output = x + y; print('x + y = $output'); }
Output
Enter x 4 Enter y 3 x + y = 7
We can also read double
values into x
and y
, and find their sum, as shown in the following program.
main.dart
import 'dart:io'; void main(){ //read number from user print('Enter x'); var x = double.parse(stdin.readLineSync()!); print('Enter y'); var y = double.parse(stdin.readLineSync()!); var output = x + y; print('x + y = $output'); }
Output
Enter x 3.14 Enter y 1.52 x + y = 4.66
ADVERTISEMENT
Summary
In this Dart Tutorial, we learned how to find the sum of given two numbers.