Average of two Numbers
To find the average of two given numbers, find their sum using Addition Operator, and divide the resulting sum by 2 (since we are finding the average of two numbers only) using Division Operator. The resulting value is the average of the two numbers.
Dart Program
In the following program, we read two numbers into x
and y
, and find their average.
main.dart
import 'dart:io'; void main(){ //read numbers from user print('Enter x'); var x = double.parse(stdin.readLineSync()!); print('Enter y'); var y = double.parse(stdin.readLineSync()!); var sum = x + y; var average = sum / 2; print('average = $average'); }
Output
Enter x 9 Enter y 14 average = 11.5
ADVERTISEMENT
Summary
In this Dart Tutorial, we learned how to find the average of two given numbers.