Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Create FindAverageOfNumbersByRecursion.dart (#5604)
Browse files Browse the repository at this point in the history
Co-authored-by: Harsh Raj <harshraj8843@gmail.com>
  • Loading branch information
charan-hash and harshraj8843 committed Mar 28, 2024
1 parent 61c7fe8 commit 3342916
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:io';

double findAverage(List<int> 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<String> numberStrings = input.split(' ');

// Convert input strings to integers
List<int> numbers = numberStrings.map(int.parse).toList();

// Calculate the average using recursion
double average = findAverage(numbers, 0, 0);

print("Average of the numbers: $average");
}

0 comments on commit 3342916

Please sign in to comment.