diff --git a/program/program/find-average-of-numbers-by-recursion/FindAverageOfNumbersByRecursion.dart b/program/program/find-average-of-numbers-by-recursion/FindAverageOfNumbersByRecursion.dart new file mode 100644 index 000000000..d9393799b --- /dev/null +++ b/program/program/find-average-of-numbers-by-recursion/FindAverageOfNumbersByRecursion.dart @@ -0,0 +1,26 @@ +import 'dart:io'; + +double findAverage(List numbers, int index, double sum) { + // Base case: when index reaches the end of the list + if (index == numbers.length) { + // Return the average + return sum / numbers.length; + } else { + // Recursive case: add current number to sum and move to the next index + return findAverage(numbers, index + 1, sum + numbers[index]); + } +} + +void main() { + print("Enter the numbers separated by space:"); + String input = stdin.readLineSync()!; + List numberStrings = input.split(' '); + + // Convert input strings to integers + List numbers = numberStrings.map(int.parse).toList(); + + // Calculate the average using recursion + double average = findAverage(numbers, 0, 0); + + print("Average of the numbers: $average"); +}